Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL, preserve quality when resizing and saving [duplicate]

I have a large image, which I resize in PIL so that it is 250 pixels wide. This is the width that it will be shown on my website.

However, the resolution is really bad. I see that it has changed the dpi from 180 to 96. If I resize the image in a program like Windows Paint then it maintains the 180 dpi. This Paint-resized image looks a lot better on my website. (The paint-resized image is 40kb while the PIL resized image is 16kb)

How do I maintain the dpi (or set it to some max that looks good on websites)

I resize this using PIL:

image = image.resize((new_width, new_height), Image.ANTIALIAS)
image.save(filepath)

I have tried:

dpi = image.info['dpi']  # (180, 180)
image.save(filepath, dpi = dpi) 

but it makes no difference. The dpi is 180, but the resolution is still bad. I'm guessing that the dpi needs to be set during resizing?

EDIT:

The issue seems to be the saving, not the resizing. Even if I start with the Paint-resized image (and therefore do not resize the image in PIL), it still saves it as the crappy quality 96 dpi (16kb) intead of keeping it as it is.

like image 915
user984003 Avatar asked Oct 15 '13 18:10

user984003


1 Answers

Thanks to the discussion with abarnert, I (he) realized that the issue was the saving, not the resizing.

And then I was able to find this, which solved the issue:

image_fullsize.save(filepath+name_fullsize, quality=95)

from here: How to adjust the quality of a resized image in Python Imaging Library?

like image 155
user984003 Avatar answered Sep 24 '22 07:09

user984003