In Python PIL, how to change image quality?

I want to degrade image quality to a few kilobytes. What is the best way to do this?

Thanks!

+6
python image compression python-imaging-library
source share
4 answers

If the image format is JPEG, here is an example:

from PIL import Image im = Image.open("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg") im.save("C:\Users\Public\Pictures\Sample Pictures\Jellyfish_compressed.jpg", quality=10) 

Links you should read:

  • [Image module] [1], in particular the “save” function, which allows you to transfer parameters corresponding to each image format.
    • Each parameter of the image format is on a different page, you can find it in the documents.
+10
source share

solvable.

I did....

 im.save( blah, quality=5) 
+1
source share

a) change the size: Image.resize(size, filter) b) explicitly convert it to JPEG (if it is not) and set the desired quality. c) use a combination of a) and b)

Whatever you do, there is a trade-off between size and quality.

+1
source share

This helped me use For Loop to resize images using PIL. The PRODUCT variable is a list that contains all the product names, but you can also use readlines () for each line in the file:

 def resize_images(self): products = PRODUCTS for name in products: try: fp = open(filename + name + ".jpg", "rb") img = Image.open(fp) img.load() img.save(filename + name + "_2" + ".jpg", quality=23) fp.close() except: print name 
0
source share

All Articles