Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

python PIL save image different size original

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...