5
votes

I'm trying to resize images while keeping aspect ratio. I use PIL's thumbnail method for this. I use Image.ANTIALIAS filter.

You can check my code here:

image = Image.open(design.design.path)
format = image.format
image = ImageOps.mirror(image)
new_size = (241, 241)
image.thumbnail(new_size, Image.ANTIALIAS)
image.save(response, format)

This code works perfectly however quality is lost after thumbnail. I can see it by zooming in on the saved image. I can see pixels at the corners of the image while I don't on the original image. This is seen even better when I print out resized image.

You can check out sample images here: http://imgur.com/a/ifZoU

Please tell me if you need anything else

2
are you making the images smaller or bigger? either way i'd recommend using the resize method rather than the thumbnail methodJames Kent
@JamesKent I'm making them smaller. I'm using thumbnail instead of resize to maintain aspect ratio. Resize does not preserve aspect ratio.Marijus
Why would resize not preserve aspect ratio? You hardcode the aspect ratio here to 1:1, and that would also be the aspect ratio if you would use resize.physicalattraction
@physicalattraction yes in this case but this is a demo code and in real application there's a few different sizes I want to resize to which are not squares.Marijus
Still, in that case resize keeps the aspect ratio you give it, which can be deduced before you resize it.physicalattraction

2 Answers

2
votes

Image.save has options for you:

img.save(fpath, 'image/png', quality=100, optimize=True) 
1
votes

If you are making thumbnails of a pixel-based image, of course you will loose quality. Re-sizing this kind of image (as opposed to vector images) simply throw information away - there is no way to recover it.

IF you need to view the image in full size, you have to preserve the original image, and keep the scaled-down version to be shown only where it is needed - use the original for everything else.

https://en.wikipedia.org/wiki/Raster_graphics