thumbor/thumbor — pull request #376 by pda (2014-10-23 09:52:01+00:00)
Title: PIL: convert palette to RGBA before resize()

Before resizing 8-bit indexed images, convert to RGBA to avoid the terrible results of nearest-neighbour resampling:

> The filter argument can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS. **If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.**
— http://effbot.org/imagingbook/image.htm#tag-Image.Image.resize

Thumbor's PIL engine does specify `ANTIALIAS`, but as stated above, PIL disregards that for `1` and `P` images.

This patch converts indexed `P` images before resize, but not 1-bit black-and-white `1` images. This is partly because I'm not sure if antialiasing is desirable given a 1-bit image (1-bit is uncommon, and perhaps it was 1-bit for a reason?), and partly because it's irrelevant to my use-case. I'm happy to add `1` images too.

Note that the output PNG was RGBA anyway, but that conversion was happening *after* the resize, resulting in worst of both worlds — nearest neighbor resampling of a highly constrained palette for poorest image quality, and then re-encoded into 32-bit color space for largest file size.

Here's the before and after of the actual image thumbnail that flagged this issue:

![palette](https://cloud.githubusercontent.com/assets/15759/4751023/7952dc00-5a99-11e4-89a3-1f45d8cd1e3a.png) ![rgb](https://cloud.githubusercontent.com/assets/15759/4751032/8436da18-5a99-11e4-9e94-7b38efa2f880.png)