Skip to content
Advertisement

How to resize image without losing pixel qualityin python?

I have an 32×32 image. I resize it for 512×512 in python, quality and view of image is not same when I resize it in paint.

original image resized-with-paint resized-with-python

What is needed to add to have same result as Paint?

from PIL import Image
im=Image.open('1.png')
im=im.resize(512,512)
im.save('resized.png')

    

Advertisement

Answer

Use:

im = im.resize((521,512), resample=Image.NEAREST)

for that effect.

It is not really a “loss of quality” that you are seeing – it is actually a difference in the interpolation method. When upsizing an image, the algorithm effectively has to “invent” new pixels to fill the bitmap raster. Some algorithms interpolate between known surrounding values, other algorithms just take the closest value – also known as “Nearest Neighbour”. There are both advantages and disadvantages – “Nearest Neighbour” will be faster and will not introduce new “in-between” colours into your resized image. On the downside, it will be slower, look more “blocky” and less smooth.

It requires some thought and experience to choose the appropriate method.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement