Last active
June 29, 2017 17:57
-
-
Save neoascetic/3978536 to your computer and use it in GitHub Desktop.
[Django] Optimize easy-thumbnails (PIL) generated images
This file contains hidden or 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
# 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 why not?
# 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
Nice! This work with Amazon S3?