Backend plugin for sorl-thumbnail that optimizes thumbnails
Copyright Peter Bengtsson, email@example.com, 2015-2016
sorl-thumbnail is a great Django library that takes your stored images and automatically convert them into desired sized thumbnails and store them with nice names in the MEDIA_ROOT. The problem is that the engines that do the resizing often doesn’t do an amazing job of optimizing them. Usually optimizing an image means carefully deleting things the human eye can’t notice anyway. This becomes incredibly relevant when the thumbnail you create is so small in resolution that the user really stands very little chance to notice.
This package, is a pluggable backend to sorl-thumbnail that attempts to do a good job of optimizing the generated thumbnail just right after it has been written to disk.
pip install optisorl.
Then add, in your Django settings:
THUMBNAIL_BACKEND = 'optisorl.backend.OptimizingThumbnailBackend'
Then review the sections below about being prepared for PNGs, GIFs and JPEGs.
optisorl uses a binary called pngquant which is a command line tool that do lossy compression of PNG images and supports alpha transparency. pngquant is BSD licensed. It’s easy to install on most systems. For example brew install pngquant or apt-get install pngquant.
What happens is that when optisorl notices that a thumbnail was created it (and stored in MEDIA_ROOT) it then takes that file and executes pngquant something like this:
pngquant -o /path/file.tmp.png --skip-if-larger -- /path/file.png
Note the --skip-if-larger which means that if the thumbnail is really really small already the resulting optimization might not be any better and it thus omits doing an optimization.
If you want to override the location of the executable pngquant you can set this setting for example:
# in settings.py or equivalent PNGQUANT_LOCATION = '/opt/special/bin/pngquant2.0'
optisorl uses gifsicle with level 3 optimization. gifsicle is GPL licensed but use is not restricted by a license. To install it use brew install gifsicle or apt-get install gifsicle.
To override where the gifsicle executable is located you can set in your settings:
# in settings.py or equivalent GIFSICLE_LOCATION = '/opt/special/bin/gifsicle'
If you want to disable all optimization of GIFs just set GIFSICLE_LOCATION (in your settings.py) to None or False.
optisorl uses mozjpeg to optimize JPEGs. It’s a great fit because it almost never reduces the quality such that human eyes can notice it. Especially when the thumbnails are relatively small. The command that we use to execute mozjpeg looks like this:
mozjpeg -outfile DESTINATION -optimise SOURCE
You can override where the executable is by setting:
# in settings.py or equivalent MOZJPEG_LOCATION = '/my/bin/mozjpeg'
Help is most welcome. At the moment…
- Does not support S3 storage
- Unable to NOT optimize images in run-time
- Not possible to override certain pngquant parameters
- Not possible to override certain gifsicle parameters
- Not possible to override certain mozjpeg parameters
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|optisorl-0.2.1-py2-none-any.whl (6.8 kB) Copy SHA256 Checksum SHA256||2.7||Wheel||Apr 13, 2016|
|optisorl-0.2.1.tar.gz (4.5 kB) Copy SHA256 Checksum SHA256||–||Source||Apr 13, 2016|