65

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.

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
bigmac
  • 833
  • 1
  • 7
  • 7

5 Answers5

104

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.

Julian
  • 4,176
  • 19
  • 40
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
  • 1
    Thanks, thats just exactly what I was looking for... Just an addition: The quality option is a kwarg so has to be passed as quality=quality – bigmac Sep 10 '09 at 15:21
  • 1
    Also note that if your original picture is `indexed`, in most cases it will increase the quality of the output if you convert it to `RGB` or `RGBA` before calling `.resize()`. – Attila O. Oct 20 '10 at 07:41
  • 3
    +1 for the quality option. But I don't get why `resize` would be better than `thumbnail`. Can someone elaborate? – Clodoaldo Neto Jan 26 '14 at 10:47
  • 1
    @ClodoaldoNeto, there's no difference. I just checked the source, and `thumbnail` calls `resize`. – Mark Ransom Apr 23 '14 at 18:54
  • The two methods differ if the final size is not same aspect ratio of the inital one. `thumbnail` fits the final size leaving empty space. `resize` stretches the image. They work same way if aspect ratio is not changed. – Vincenzooo May 19 '16 at 02:51
  • 1
    Also they have different resampling method, but this can be changed. – Vincenzooo May 19 '16 at 03:46
  • 5
    From [the documentation](http://pillow.readthedocs.io/en/3.2.x/handbook/image-file-formats.html#jpeg): `quality`: The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality. – Jens Jun 20 '16 at 16:01
  • How to reduce the quality without saving the image? – PlsWork Jun 12 '19 at 23:11
48

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 3x3 image that looks like this:

2 2 2
2 0 2
2 2 2

and I resize it to 2x2, 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.

jcdyer
  • 18,616
  • 5
  • 42
  • 49
16

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.

teewuane
  • 5,524
  • 5
  • 37
  • 43
DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
  • 32
    "The image quality, on a scale from 1 (worst) to 95 (best). The default is 75. Values above 95 should be avoided; 100 completely disables the JPEG quantization stage." http://www.pythonware.com/library/pil/handbook/format-jpeg.htm – Izz ad-Din Ruhulessin Feb 27 '12 at 02:55
  • @Izzad-DinRuhulessin The link seems broken. Maybe you could recite it. – Abhyudai Dec 16 '19 at 18:48
1

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

jsdev
  • 672
  • 6
  • 14
-1

One way to achieve better quality is to do the downscaling in two steps. For example if your original image is 1200x1200 and you need to resize it to 64x64 pixels, then on the first step downscale it to somewhere in the middle of these two sizes:

1200x1200 -> 600x600 -> 64x64

plaes
  • 31,788
  • 11
  • 91
  • 89