8

I am using PIL to rotate some images, and I noticed that the output file is much smaller, so I tried a test: Do nothing to the file, except save the exif info (because I know that PIL Image will by default not save the exif info). So here is my test code:

    from PIL import Image
    test = Image.open('my_image_file.jpg')
    holdexif = test.info["exif"]
    test.save('my_saved_image_file.jpg',"jpeg",exif=holdexif)

When I do the above my_saved_image_file.jpg is significantly smaller (441 KB) than the original my_image_file (1.83 MB). Why is that? What is missing?

When I look at various properties of both files, they appear to be identical. Both are 56 inches x 27 inches, both are 72 Pixels per inch, both are 4032 x 1960 pixels. I am by no means an expert when it comes to image files. Based on these things that I have examined (size, resolution, and the appearance of the images) the files appear to me to be the same. From what I can see/understand, only the file size on disk is different. What else should I be looking at? What else may be different?

I also tried the quality= kwarg in the save:

    test.save('my_saved_image_file.jpg',"jpeg",exif=holdexif,quality=95)

which gave me a file (1.73 MB) almost as large as the original (1.83 MB). But I have no idea what is different to account for the larger size (and I don't understand what makes the "quality" better; the images appear the same to me). I am trying to understand what exactly is different between the two files, so that I can make a decision. Perhaps the smaller size is perfectly fine for my purposes.

8
  • 1
    The PILs IMAGE is doing the compression. you can check github.com/python-pillow/Pillow/blob/master/src/PIL/… here Commented Dec 26, 2019 at 3:27
  • 1
    Thanks. Is the compression causing any loss of actual quality? It doesn't appear to me. But as I mentioned, for quality I only know to look at the size, resolution, and appearance to my eye on the screen. Is there anything lost in the compression? If so, what. Thanks. Commented Dec 26, 2019 at 3:30
  • 1
    diffchecker.com/image-diff Let's try checking here. In the difference tab, Commented Dec 26, 2019 at 3:32
  • I'm reading the code, trying to find how it decodes the image from file and putting it back as a file. Commented Dec 26, 2019 at 3:33
  • Thanks. That diffchecker is nice, but it shows no differences (using quality=95%, will try some other levels). I am also reading this to see if I can understand the effect of compression better: fotoforensics.com/tutorial-estq.php Apparently there is some loss with the compression, it just may not be obvious to the eye. Commented Dec 26, 2019 at 3:39

1 Answer 1

4

The answer, as Charanjit pointed out, is in the amount of compression (which is controlled with the quality kwarg of the .save() method). More compression means a smaller file and less "quality".

Regarding "quality", this means that, although the image size and image resolution may be the same, the crispness of object edges, and color differentiation, within the image, will be reduced (possibly not apparent to the eye) by compression.

There is a good discussion of jpeg compression here: Understanding JPEG Quality.

There is also another stackoverflow answer that addresses the issue of trying to save jpeg file with the same quality as the original: https://stackoverflow.com/a/4355281/1639359 by setting kwarg quality='keep' (instead of quality=N where N is an integer between 1 and 100 %).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.