Python is a great and accessible programming language for automation scripts and components of web services. Whenever you code in this or another programming language, you can include Python libraries into your project and utilize their classes and methods, thanks to its object-oriented structure. It’s very powerful at doing mathematical calculations and it works with dynamic data types. Python is also used for image compression and image resize operations.
In this article, we’ll run through the most popular Python libraries that you can use on your website to optimize and resize images on the fly. All these libraries are open source, so you can just take them and use their methods and functions to deal with images, or you can fork the libraries on GitHub and customize them for your needs or optimize them, as Alex Karpinsky did with Pillow. A bit more on that later in this article.
While the algorithms for JPEG resizing & compression are quite complicated and include functional math and operations like convolution and window functions, we won’t go that deep today; we’ll just quickly go over the interfaces for developers and do some efficiency comparisons.
Image optimization is a must-have for any website that has pictures that take up to 60% of the traffic. It’s commercially viable for online stores (with a picture traffic of up to 80%), because the shorter the loading time, the more pages users will open. In turn, it directly affects the average shopping cart value for both existing users and first-time visitors.
Python scripts are usually shorter than the same scripts in functionality made with the C language family, Java and other popular programming languages. Therefore, it’s easier to read the source code and understand what exactly Python functions are doing.
In the beginning, there was PIL, an open-source Python Imaging Library. It works with Python version 2, which was discontinued in 2020. It offers methods to open, manipulate (including resizing and compression) and save image files.
It’s a well-known library, but it doesn’t use the most optimal way of resizing images. To make a long story short, there are three interpolation resizing methods (which feature window functions). Bilinear, a simple one, may produce a few resizing artifacts but is good enough for most images. Bicubic is a more advanced function that preserves more sharp edges when resizing and works with 4x4 pixel cubes. Lanczos, aka Antialias, has the widest functionality among these three and is overkill for most cases as it works in the 8x8 pixel neighborhood.
For some reason, PIL uses Lanczos by default, and therefore it’s very slow in resizing image tasks.
Among all the alternatives, the Pillow library is one of the most functional and popular image processing libraries in Python. It’s a successor to PIL and it works with about 30 types of image files, including all popular image formats used on the web, such as JPEG, PNG, GIF and WebP.
It features all types of downscaling and filters, including Bilinear, Bicubic and Lanczos (Antialias). You can resize, apply Python image compression for JPEGs and other lossy formats, and perform other types of image manipulations with it.
The original library has been forked multiple times and some of these branches widened the functionality or improved image processing performance.
Uploadcare uses Pillow-SIMD at its image-processing core and it’s one of the most efficient Python libraries. It was forked from the original Pillow and brought to the next processing efficiency level by Alex Karpinsky, an Uploadcare developer. He is an author of unique algorithms and techniques that, when combined, reduce server workload by sixfold, meaning that to process the same amount of resizing operations requires six times fewer servers with this library.
Examples of these techniques include using box filtering to reduce an image size by an integer number (e.g., from 4000 by 4000 px to 200 by 200 px—the multiplier is 20), along with a convolution resize that requires more resources but can deal with fractional resizing multipliers better (e.g., from 200 by 200 px to 180 by 180 px—the multiplier is 0.9).
Also, the Single Instruction Multiple Data (SIMD) approach allows for parallel, faster computing over multimedia transformations, such as image resize algorithms. It works on x86 processor architecture so it’s platform-tethered, but if you use Uploadcare as a service, all computation is done in the cloud.
The processing time is drastically improved in comparison with other Python libraries and even native x86 image processing instructions.
By the way, Pillow-SIMD is also open source and free to use, so you are welcome to try it out in your own project.
There are some other libraries and tools that you can use in your Python application or a web project made in PHP, C# or another programming language.
- Img4web is a Python script that optimizes JPEG, PNG, and animated GIFs for your website. The script can produce lossless and slightly compressed images, which nonetheless reduces the loading time of pages with pictures.
- smush.py, a Python command-line tool that functions as a lossless image optimizer for online images. Smush.py originated from Yahoo's service.
- Tinify, a Python package for compressing and optimizing JPEG and PNG images. It offers a nice API that allows you to access its methods from within another application.
- scikit-image, an image-processing Python library with a versatile set of features, including image rescale, optimization techniques and transformations.
Having a Pillow-SIMD library at hand is a great help. However, you’ll probably get more reliable results with less time and expense spent on development if you try out Uploadcare, which uses this library at its core anyway. Image processing on the fly is not just a code and algorithms, but a means to deliver images to end users (i.e., your website visitors or app users). And this is a way more complex task, which involves DevOps resources and a CDN infrastructure.
Plus, after installing Uploadcare on your website, you’ll be able to address image resizing/compressions functions via HTML parameters, like this:
This is the easiest way to change the compression:
All images will be stored in the cloud, and these operations won’t destroy your original images.
:value can be
lighter— compress images without quality loss. ≈80% file size.
lightest— a good parameter for retina displays and ultra-high resolutions. ≈50% file size.
smart— automatic content-aware mode to compress images.
If your JPEG image is already compressed more than you specified, the size won’t increase, so you can’t go wrong saving on traffic with Uploadcare.
This article is about Python libraries, so I won’t go any further with Uploadcare methods here, but if you’d like to learn more, read the documentation chapter on image resizing and compression.
Also, you can get a free trial and test it on your own along with experimenting with Python libraries and their methods:
Python is a great language for resizing website pictures, but you can resize images without PIL or any other library by using all-around services for image management and delivery. That’s especially relevant if your business depends on picture loading speed and quality.