forked from anujagnivanshi/awsbit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython_resizeimage
More file actions
57 lines (45 loc) · 1.71 KB
/
Python_resizeimage
File metadata and controls
57 lines (45 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import print_function
import json
import urllib
import boto3
import PIL
from PIL import Image
from io import BytesIO
from os import path
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
width_size = 600
width_size30 = 30
#extension = path.splitext(key)[1].lower()
try:
print("Starting s3 get object")
response = s3.get_object(Bucket=bucket, Key=key)
print("Starting s3 get object2")
#obj_body = response.get()['Body'].read()
obj_body = response['Body'].read()
print("read s3 get object complete")
#if extension in ['.jpeg', '.jpg']:
format = 'JPEG'
#if extension in ['.png']:
# format = 'PNG'
img = Image.open(BytesIO(obj_body))
wpercent = (width_size / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((width_size, hsize), PIL.Image.ANTIALIAS)
buffer = BytesIO()
img.save(buffer, format)
buffer.seek(0)
print("upload starting")
# Uploading the image
new_key = key + '_600'
s3.put_object(Body=buffer, Bucket='mydemokelly', Key=new_key)
print("Done")
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e