2.7.0

Sane Plugin

The Sane plugin has now been split into its own repo:https://github.com/python-pillow/Sane .

Png text chunk size limits

To prevent potential denial of service attacks using compressed textchunks, there are now limits to the decompressed size of text chunksdecoded from PNG images. If the limits are exceeded when opening a PNGimage a ValueError will be raised.

Individual text chunks are limited toPIL.PngImagePlugin.MAX_TEXT_CHUNK, set to 1MB bydefault. The total decompressed size of all text chunks is limited toPIL.PngImagePlugin.MAX_TEXT_MEMORY, which defaults to64MB. These values can be changed prior to opening PNG images if youknow that there are large text blocks that are desired.

Image resizing filters

Image resizing methods resize() andthumbnail() take a resample argument, which tellswhich filter should be used for resampling. Possible values are:PIL.Image.NEAREST, PIL.Image.BILINEAR,PIL.Image.BICUBIC and PIL.Image.ANTIALIAS.Almost all of them were changed in this version.

Bicubic and bilinear downscaling

From the beginning BILINEAR andBICUBIC filters were based on affine transformationsand used a fixed number of pixels from the source image for every destinationpixel (2x2 pixels for BILINEAR and 4x4 forBICUBIC). This gave an unsatisfactory result fordownscaling. At the same time, a high quality convolutions-based algorithm withflexible kernel was used for ANTIALIAS filter.

Starting from Pillow 2.7.0, a high quality convolutions-based algorithm is usedfor all of these three filters.

If you have previously used any tricks to maintain quality when downscaling withBILINEAR and BICUBIC filters(for example, reducing within several steps), they are unnecessary now.

Antialias renamed to Lanczos

A new PIL.Image.LANCZOS constant was added instead ofANTIALIAS.

When ANTIALIAS was initially added, it was the onlyhigh-quality filter based on convolutions. It’s name was supposed to reflectthis. Starting from Pillow 2.7.0 all resize method are based on convolutions.All of them are antialias from now on. And the real name of theANTIALIAS filter is Lanczos filter.

The ANTIALIAS constant is left for backward compatibilityand is an alias for LANCZOS.

Lanczos upscaling quality

The image upscaling quality with LANCZOS filter wasalmost the same as BILINEAR due to bug. This has been fixed.

Bicubic upscaling quality

The BICUBIC filter for affine transformations producedsharp, slightly pixelated image for upscaling. Bicubic for convolutions ismore soft.

Resize performance

In most cases, convolution is more a expensive algorithm for downscalingbecause it takes into account all the pixels of source image. ThereforeBILINEAR and BICUBIC filters’performance can be lower than before. On the other hand the quality ofBILINEAR and BICUBIC was close toNEAREST. So if such quality is suitable for your tasksyou can switch to NEAREST filter for downscaling,which will give a huge improvement in performance.

At the same time performance of convolution resampling for downscaling has beenimproved by around a factor of two compared to the previous version.The upscaling performance of the LANCZOS filter hasremained the same. For BILINEAR filter it has improved by1.5 times and for BICUBIC by four times.

Default filter for thumbnails

In Pillow 2.5 the default filter for thumbnail() waschanged from NEAREST to ANTIALIAS.Antialias was chosen because all the other filters gave poor quality forreduction. Starting from Pillow 2.7.0, ANTIALIAS has beenreplaced with BICUBIC, because it’s faster andANTIALIAS doesn’t give any advantages afterdownscaling with libjpeg, which uses supersampling internally, not convolutions.

Image transposition

A new method PIL.Image.TRANSPOSE has been added for thetranspose() operation in addition toFLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM,ROTATE_90, ROTATE_180,ROTATE_270. TRANSPOSE is an algebratranspose, with an image reflected across its main diagonal.

The speed of ROTATE_90, ROTATE_270and TRANSPOSE has been significantly improved for largeimages which don’t fit in the processor cache.

Gaussian blur and unsharp mask

The GaussianBlur() implementation has been replacedwith a sequential application of box filters. The new implementation is based on“Theoretical foundations of Gaussian convolution by extended box filtering” fromthe Mathematical Image Analysis Group. As UnsharpMask()implementations use Gaussian blur internally, all changes from this chapterare also applicable to it.

Blur radius

There was an error in the previous version of Pillow, where blur radius (thestandard deviation of Gaussian) actually meant blur diameter. For example, toblur an image with actual radius 5 you were forced to use value 10. This hasbeen fixed. Now the meaning of the radius is the same as in other software.

If you used a Gaussian blur with some radius value, you need to divide thisvalue by two.

Blur performance

Box filter computation time is constant relative to the radius and dependson source image size only. Because the new Gaussian blur implementationis based on box filter, its computation time also doesn’t depends on the blurradius.

For example, previously, if the execution time for a given test image was 1second for radius 1, 3.6 seconds for radius 10 and 17 seconds for 50, now blurwith any radius on same image is executed for 0.2 seconds.

Blur quality

The previous implementation takes into account only source pixels within2 * standard deviation radius for every destination pixel. This was not enough,so the quality was worse compared to other Gaussian blur software.

The new implementation does not have this drawback.

TIFF Parameter Changes

Several kwarg parameters for saving TIFF images were previouslyspecified as strings with included spaces (e.g. ‘x resolution’). Thiswas difficult to use as kwargs without constructing and passing adictionary. These parameters now use the underscore character insteadof space. (e.g. ‘x_resolution’)