Skip to content

Instantly share code, notes, and snippets.

@andreinechaev
Created February 12, 2016 17:28
Show Gist options
  • Save andreinechaev/ac79e8d9b3be10eec876 to your computer and use it in GitHub Desktop.
Save andreinechaev/ac79e8d9b3be10eec876 to your computer and use it in GitHub Desktop.
Image resizing on S3
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