shrinkpix — Shrink Your Website's Images

This program reduces the filesize of images for faster (and politer) online viewing. With it, you can shrink all the images in your entire website in a single step. Though still somewhat experimental, its results are good enough to be used for nearly all of the images at its home website, including those in the image galleries listed here.

Please note up front: the preceding brands this program "experimental" on purpose. As of its latest Sep-2020 release, shrinkpix does well on JPEGs and others, but can degrade the quality of some PNG images noticeably (auto-conversions of color schemes are a prime suspect), and its originator has neither time nor interest in improving it soon. If you do, please consider this program a basis for future enhancements, and an example of the sorts of tools available in the Pillow image library. As is, this script's results are usable far more often than not, but you should inspect them post run, and replace any subpar PNGs as needed.

Overview

shrinkpix reduces an image's size by first saving the original version, and then applying transformations until either the desired size is met, or no more transformations remain. Although its results may serve a variety of purposes, this program is primarily intended for reducing the filesize of a website's images posted online. In this role, the smaller images it creates can be viewed quicker, and are friendlier to users with limited or metered bandwidth.

This program runs locally on your computer from a console, IDE, or script; can be used to shrink both individual images and all the images in a folder or tree; and comes with utilities to restore and collect the original unshrunk images backed up by the main script. Its shrinking techniques are still open to improvement as shipped, but you may find its code useful for your own goals, and its current results adequate for your own sites.

Per results so far, it's not unusual for this program to shrink an image's filesize from 6M to 200-300k—a 20-30x reduction, with proportionate decreases in load times and bandwidth use, and with no obvious decline in visual quality at normal viewing sizes. Naturally, this also reduces the heft of download packages that embed many images; in one case, a 250M zipfile shrunk to 110M.

Perhaps most importantly, this program can make the impractical practical: for sites like learning-python.com with hundreds (or thousands) of images, this program's rare quality reductions are more than outweighed by the time savings of its conversion automation.

Logistics

To get shrinkpix, download its zipfile from here, and unzip it to a folder on your device. Usage details are available in the top-of-file docstring of the main script, described in the next section.

This program is shipped as Python source code that you run from a console or IDE. It uses and requires both Python 3.X and the Pillow third-party library, and should work on any platform that supports both. This program also uses the piexif third-party library in a minor role, but ships this library's code with its own. Specifically, Pillow is used for most image processing, but piexif is required to change dimension values in propagated Exif tags after resizes.

Fine print: in testing to date, shrinkpix has been verified to work correctly on Mac OS 10.11 and later, and Windows 10; using Python 3.5 through 3.8; with Pillow 4.2, 5.1, and 7.0. Its results have no visual-quality difference across these platforms and software versions (and usually yield byte-for-byte identical image files on the same platform). shrinkpix is also expected to work on Linux, Android, and others.

Resources

This system consists of three Python 3.X source-code scripts:
shrinkpix.py
The main script: shrink images, save originals
restore-unshrunk-images.py
Utility: restore original images saved by the shrinker
collect-unshrunk-images.py
Utility: collect original images saved by the shrinker

Additional resources:

This program was last changed: September 30, 2020.

Its latest release borrows two work-arounds from thumbspage: silence a bogus DOS warning or error in the Pillow library, and fix some miscoded Exif tags to avoid failures in the piexif library; more details in thumbspage's user guide here and here, respectively. As shipped, shrinkpix's restore utility now also retains all items instead of discarding duplicates; see the restore script's docstring. See the main script's docstring for all versions' details.

Related

For a program that generates image galleries that may benefit from shrinkpix resizing when viewed online, see the thumbspage program, including its usage note on image-size issues. For more programs from the makers of shrinkpix, visit the examples page.



[Home page] Books Code Blog Python Author Train Find ©M.Lutz