-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
69 lines (57 loc) · 2.2 KB
/
Copy pathlambda_function.py
File metadata and controls
69 lines (57 loc) · 2.2 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
58
59
60
61
62
63
64
65
66
67
68
69
import json
import logging
import boto3
from PIL import Image
import io
import os
# Configure logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
s3_client = boto3.client('s3')
target_bucket = os.environ['TARGET_BUCKET_NAME']
def lambda_handler(event, context):
logger.info("Lambda function started")
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
try:
# Download the image from S3
logger.info(
f"Downloading image from bucket: {bucket_name}, key: {object_key}")
image_object = s3_client.get_object(Bucket=bucket_name, Key=object_key)
image_data = image_object['Body'].read()
# Open the image using Pillow
with Image.open(io.BytesIO(image_data)) as img:
# Define the crop box (left, upper, right, lower)
crop_box = (0, 0, 1080, 1080)
cropped_img = img.crop(crop_box)
logger.info(
f"Successfully cropped the image"
)
# Save the cropped image to a bytes buffer
buffer = io.BytesIO()
cropped_img.save(buffer, format="PNG")
buffer.seek(0)
logger.info(
f"Successfully saved the cropped image to bytes buffer"
)
# Upload the cropped image back to S3
cropped_object_key = f"cropped-{object_key}"
s3_client.put_object(Bucket=target_bucket,
Key=cropped_object_key,
Body=buffer,
ContentType='image/png')
logger.info(
f"Successfully cropped and uploaded {cropped_object_key} to {target_bucket}"
)
return {
'statusCode':
200,
'body':
f'Successfully cropped and uploaded {cropped_object_key} to {bucket_name}'
}
except Exception as e:
logger.error(f"Error processing image: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps(f"Error processing image: {str(e)}")
}