1

I´m working on a project with PIL in python. Simply by opening and saving an image makes the output image bigger (in Bytes) than the original, maintaining the same resolution, and i don´t know why...

from PIL import Image
img = Image.open("photo.png")
img.save("photo2.png", "PNG")

result from code above

Does any one have any idea why this happens? i need them to be exactly the same.

Carlos Ribeiro
  • 133
  • 2
  • 8

2 Answers2

2

PNG is a compressed lossless format. The original image was probably saved with different compression settings.

Looking at the documentation you could try:

img.save("photo2.png", "PNG", optimize=True)

or

img.save("photo2.png", "PNG", compress_level=9)

By default, compress_level=6 is used.

Note that the optimize option includes setting the compression level to 9. But it also tries to find optimal encoder settings.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • 1
    it did not solve the problem... Indeed the original image must have different compression settings, not available in the Python Imaging Library (PIL). What i did was using photo2.png as the original... ty any awy – Carlos Ribeiro Nov 11 '15 at 16:13
  • The `libpng` library has losts of possible settings other than compression. PIL can probably not use all of them. – Roland Smith Nov 11 '15 at 21:57
1

I guess you don't change the image, otherwise the size would be different. If you don't change the image why don't you just copy it?

shutil.copyfile(source, destination)  
Mace
  • 1,355
  • 8
  • 13