Skip to content

Instantly share code, notes, and snippets.

@neoascetic
Last active June 29, 2017 17:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neoascetic/3978536 to your computer and use it in GitHub Desktop.
Save neoascetic/3978536 to your computer and use it in GitHub Desktop.
[Django] Optimize easy-thumbnails (PIL) generated images
# On Debian, you need to install these packages:
# jpegoptim optipng pngcrush advancecomp
import subprocess
from os.path import splitext
from django.dispatch import receiver
from easy_thumbnails.signals import saved_file, thumbnail_created
# on-save image optimization
@receiver(saved_file)
def optimize_file(sender, fieldfile, **kwargs):
optimize(fieldfile.path)
# thumbnail optimization
@receiver(thumbnail_created)
def optimize_thumbnail(sender, **kwargs):
optimize(sender.path)
def optimize(path):
# taken from trimage (http://trimage.org/)
runString = {
".jpeg": u"jpegoptim -f --strip-all '%(file)s'",
".jpg": u"jpegoptim -f --strip-all '%(file)s'",
".png": u"optipng -force -o7 '%(file)s' && advpng -z4 '%(file)s' && pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time '%(file)s' '%(file)s.bak' && mv '%(file)s.bak' '%(file)s'"
}
ext = splitext(path)[1].lower()
if ext in runString:
subprocess.Popen(runString[ext] % {'file': path}, shell=True)
@leoberdu
Copy link

Nice! This work with Amazon S3?

@neoascetic
Copy link
Author

@leoberdu why not?

@rsp2k
Copy link

rsp2k commented Mar 16, 2016

# Make this optional
try:
    ext = splitext(path)[1].lower()
    if ext in runString:
        subprocess.Popen(runString[ext] % {'file': path}, shell=True)
except:
    pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment