Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple python code to compress (jpeg/png) images uploaded to S3. In this use case I want to compress down all images that are over 500 KB down to 80% quality which brings the images down to about 100KB. Feel free to muck with the values.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from boto.s3.connection import S3Connection
from cStringIO import StringIO
from PIL import Image as pil
AWS_KEY = '[AWS KEY HERE]'
AWS_SECRET = '[AWS SECRET HERE]'
BUCKET_NAME = '[BUCKET NAME]'
def start_compressing(dry_run=False):
conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
i = 0
for img in bucket.list():
if img.size > 500000:
if not dry_run:
compress_image(img, i)
i += 1
if dry_run:
print 'Will compress {0} images'.format(i)
else:
print 'Compressed {0} images'.format(i)
def compress_image(pic, index):
print u'{0}: compressing image: {1}'.format(index, pic.name)
input_file = StringIO(pic.get_contents_as_string())
img = pil.open(input_file)
tmp = StringIO()
img.save(tmp, 'JPEG', quality=80)
tmp.seek(0)
output_data = tmp.getvalue()
headers = dict()
headers['Content-Type'] = 'image/jpeg'
headers['Content-Length'] = str(len(output_data))
pic.set_contents_from_string(output_data, headers=headers, policy='public-read')
tmp.close()
input_file.close()
if __name__ == "__main__":
if len(sys.argv) > 1:
command = sys.argv[1]
if command == 'dry_run':
start_compressing(dry_run=True)
else:
print 'Invalid command. For a dry run please run: python compress_s3_images.py dry_run'
else:
start_compressing()
sys.exit()
@ryanniemeyer

This comment has been minimized.

Copy link

ryanniemeyer commented Feb 6, 2013

Here's a snippet for invalidating CloudFront paths, that can be useful in times like these :) https://gist.github.com/ryanniemeyer/4720393

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.