Pil: Image Resizing : Algorithm Similar To Firefox's
Solution 1:
I resized the "original" with Python and found the same results as you did. I also resized the "original" with GIMP and I got the same (if not inferior) quality. This made me suspect that Firefox cheats. Possibly it converts to RGB ("original" mode is indexed color). Thus the following code:
import Image
im=Image.open("beta-icon.gif")
im = im.convert("RGB")
im=im.resize((36,36), Image.ANTIALIAS)
im.save("q5.png")
The result is almost as good as that of Firefox.
Solution 2:
It looks like the RGB and then ANTIALIS looks the best. Any other recommendations?
No, that is indeed the expected result. Any resizing done in the original limited palette mode is likely to produce jaggy rubbish because of the lack of available in-between colours in the palette; and ANTIALIAS is the only resize filter that is intended to be used for downscaling: BILINEAR and BICUBIC really do only take two pixels per axis and blend between them, which is fine for upscaling but doesn't work at all when one or both axes are downscaled.
Unfortunately thumbnail() has never really worked properly so you have to do it yourself.
Solution 3:
Try using the resize()
method instead of thumbnail()
. In my experience, they behave very differently.
Also, your code shows reading a .gif, but your original is .png. Make sure you really have all the original data before you start reducing it.
Solution 4:
The image posted is an indexed image. Its mode is 'P'
if you read it via PIL. The PIL documentation on Image.resize() for the resample parameter states that:
If the image has mode “1” or “P”, it is always set to PIL.Image.NEAREST
So PIL will use nearest filter for resizing indexed images, which is a low quality filter.
The right way is to convert the image mode to RGB
and then use resize with a high-quality filter such as Image.LANCZOS
. By the way, Image.ANTIALIAS
is now the same as Image.LANCZOS
, source here.
import Image
img = Image.open("beta-icon.gif").convert("RGB")
img = img.resize((36,36), Image.LANCZOS)
img.save("img-resized.png")
Post a Comment for "Pil: Image Resizing : Algorithm Similar To Firefox's"