8

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

Thanks!

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

4 Answers4

13

If the picture format is JPEG, here's 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)

The references you need to be reading are:

  • [The Image module][1], particularly the "save" function, which allows you to pass in options relevant for each image format.
    • Each image format's options are in a different page, you can find it in the docs.
Asim Ihsan
  • 1,501
  • 8
  • 18
  • PNG accepts an "optimize" flag. You need to check each individual image format's docs to determine what optimization flags you can use. – Asim Ihsan Dec 04 '10 at 10:24
  • 1
    FWIW, the OP isn't big on reading the documentation -- he's got S.O. – martineau Dec 04 '10 at 11:55
  • @martineau also FWIW, I find the PIL documentation fairly spotty. IMX, you can't really just hack around with `dir` / `help` / `doc` and get answers as often as you'd like. Also, for example replacing `png` with `jpg` in Asymptote's link yields a 404. – Karl Knechtel Dec 04 '10 at 12:16
  • @Karl Knechtel: It may not be the greatest documentation, but the answer to this question is in it. For `im.save()` it says "Keyword options can be used to provide additional instructions to the writer. If a writer doesn't recognize an option, it is silently ignored. The available options are described later in this handbook". If you then look in the JPEG appendix it says "The save method supports the following options: quality - 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". – martineau Dec 04 '10 at 17:39
  • @Karl Knechtel: Here's a link for the [PNG appendix](http://www.pythonware.com/library/pil/handbook/format-png.htm) in the online manual. – martineau Dec 04 '10 at 17:52
  • @asim ihsan compressing this way.. image looses orientation data .. and pic at times get saved inverted/flipped etc... how to keep the exif data? – vijay shanker Aug 27 '14 at 12:10
  • @vijayshanker I can confirm that using PIL in this way does not retain EXIF metadata. You can use another Python module like `pyexiv2` to manually copy over the EXIF after creating the new file. For more information see: http://stackoverflow.com/questions/17042602/preserve-exif-data-of-image-with-pil-when-resizecreate-thumbnail – Asim Ihsan Aug 27 '14 at 16:15
1

Solved.

I did....

im.save( blah, quality=5)
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
1

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.

khachik
  • 28,112
  • 9
  • 59
  • 94
0

This worked for me to use a For Loop to resize images using PIL. The variable PRODUCTS is a list that had all product names in it, but you can also use readlines() for each line in a file to do so:

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 
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111