How to adjust the quality of a resized image in Python Imaging Library?
Question:
I am working on PIL and need to know if the image quality can be adjusted while resizing or thumbnailing an image. From what I have known is the default quality is set to 85. Can this parameter be tweaked during resizing?
I am currently using the following code:
image = Image.open(filename)
image.thumbnail((x, y), img.ANTIALIAS)
The ANTIALIAS
parameter presumably gives the best quality. I need to know if we can get more granularity on the quality option.
Answers:
Use PIL’s resize
method manually:
image = image.resize((x, y), Image.ANTIALIAS) # LANCZOS as of Pillow 2.7
Followed by the save method
quality_val = 90
image.save(filename, 'JPEG', quality=quality_val)
Take a look at the source for models.py
from Photologue to see how they do it.
ANTIALIAS is in no way comparable to the “85” quality level. The ANTIALIAS parameter tells the thumbnail method what algorithm to use for resampling pixels from one size to another. For example, if I have a 3×3 image that looks like this:
2 2 2
2 0 2
2 2 2
and I resize it to 2×2, one algorithm might give me:
2 2
2 2
because most of the pixels nearby are 2s, while another might give me:
1 1
1 1
in order to take into account the 0 in the middle. But you still haven’t begun to deal with compression, and won’t until you save the image. Which is to say that in thumbnailing, you aren’t dealing with gradations of quality, but with discrete algorithms for resampling. So no, you can’t get finer control here.
If you save to a format with lossy compression, that’s the place to specify levels of quality.
Don’t confuse rescaling and compression.
For the best quality you have to use both. See the following code:
from PIL import Image
image = Image.open(filename)
image.thumbnail((x, y), Image.ANTIALIAS)
image.save(filename, quality=100)
In this way I have very fine thumbs in my programs.
One way to achieve better quality is to do the downscaling in two steps. For example if your original image is 1200×1200 and you need to resize it to 64×64 pixels, then on the first step downscale it to somewhere in the middle of these two sizes:
1200×1200 -> 600×600 -> 64×64
Antialias n set quality like 90
img = img.resize((128,128),Image.ANTIALIAS)
img.save(os.path.join(output_dir+'/'+x,newfile),"JPEG",quality=90)
http://www.dzone.com/snippets/resize-thousands-images-python
I am working on PIL and need to know if the image quality can be adjusted while resizing or thumbnailing an image. From what I have known is the default quality is set to 85. Can this parameter be tweaked during resizing?
I am currently using the following code:
image = Image.open(filename)
image.thumbnail((x, y), img.ANTIALIAS)
The ANTIALIAS
parameter presumably gives the best quality. I need to know if we can get more granularity on the quality option.
Use PIL’s resize
method manually:
image = image.resize((x, y), Image.ANTIALIAS) # LANCZOS as of Pillow 2.7
Followed by the save method
quality_val = 90
image.save(filename, 'JPEG', quality=quality_val)
Take a look at the source for models.py
from Photologue to see how they do it.
ANTIALIAS is in no way comparable to the “85” quality level. The ANTIALIAS parameter tells the thumbnail method what algorithm to use for resampling pixels from one size to another. For example, if I have a 3×3 image that looks like this:
2 2 2
2 0 2
2 2 2
and I resize it to 2×2, one algorithm might give me:
2 2
2 2
because most of the pixels nearby are 2s, while another might give me:
1 1
1 1
in order to take into account the 0 in the middle. But you still haven’t begun to deal with compression, and won’t until you save the image. Which is to say that in thumbnailing, you aren’t dealing with gradations of quality, but with discrete algorithms for resampling. So no, you can’t get finer control here.
If you save to a format with lossy compression, that’s the place to specify levels of quality.
Don’t confuse rescaling and compression.
For the best quality you have to use both. See the following code:
from PIL import Image
image = Image.open(filename)
image.thumbnail((x, y), Image.ANTIALIAS)
image.save(filename, quality=100)
In this way I have very fine thumbs in my programs.
One way to achieve better quality is to do the downscaling in two steps. For example if your original image is 1200×1200 and you need to resize it to 64×64 pixels, then on the first step downscale it to somewhere in the middle of these two sizes:
1200×1200 -> 600×600 -> 64×64
Antialias n set quality like 90
img = img.resize((128,128),Image.ANTIALIAS)
img.save(os.path.join(output_dir+'/'+x,newfile),"JPEG",quality=90)
http://www.dzone.com/snippets/resize-thousands-images-python