Backend plugin for sorl-thumbnail that optimizes thumbnails
Project description
Copyright Peter Bengtsson, mail@peterbe.com, 2015-2016
License: BSD
About optisorl
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.
Installation
First, simply 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.
Optimizing PNGs
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'
Optimizing GIFs
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.
Optimizing JPEGs
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'
For an example of what kind of results you can get with mozjpeg see this blog post: Examples of mozjpeg savings. Also see blog post mozjpeg installation and sample.
Limitations
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file optisorl-0.2.1.tar.gz
.
File metadata
- Download URL: optisorl-0.2.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b18d8a270075aa0fe9588790162c03a80e822237e3346a2817d6e82236f1a49 |
|
MD5 | 8a330f29e4373193376494a427fa2f43 |
|
BLAKE2b-256 | d2634bbbaa95e5ccf5f39ea723714aa2e1ae861e1ce8cf408a874d7aed5d7aeb |
File details
Details for the file optisorl-0.2.1-py2-none-any.whl
.
File metadata
- Download URL: optisorl-0.2.1-py2-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 808f22b6d4da608a255f40792e80cf8483f443425dbcb5f18ebe236295c19bbf |
|
MD5 | a23d7148e36a26e0dcb5db205ca961d6 |
|
BLAKE2b-256 | e91917cf3bca1ba63dc633f5d146057f06096946a5c49f6ba4990f860d89010f |