Created
February 12, 2016 17:28
-
-
Save andreinechaev/ac79e8d9b3be10eec876 to your computer and use it in GitHub Desktop.
Image resizing on S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import botocore.exceptions | |
from PIL import Image | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket('ajs3') | |
size = 200, 200 | |
def resize(filename): | |
img = Image.open(filename) | |
img.thumbnail(size) | |
img.save('thumbnail_' + filename, 'PNG') | |
if __name__ == '__main__': | |
for b in s3.buckets.all(): | |
print(b.name) | |
for obj in bucket.objects.all(): | |
if '.png' in obj.key: | |
fn = obj.key.split('/')[-1] | |
try: | |
bucket.download_file(obj.key, fn) | |
resize(fn) | |
except botocore.exceptions.DataNotFoundError as e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment