Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Since 10.0.1, saving an image as a JPEG does not always result in files of the same size #7477

Closed
johnnyleitrim opened this issue Oct 18, 2023 · 11 comments · Fixed by #7472
Closed
Labels

Comments

@johnnyleitrim
Copy link

What did you do?

Using Pillow 10.0.1 and above, saving an image as a JPEG does not always result in the same number of bytes being written.

What did you expect to happen?

Saving the same image multiple times as a JPEG would result in files of the same size.

What actually happened?

The file sizes differ

What are your OS, Python and Pillow versions?

  • OS: macOS Ventura 13.6
  • Python: 3.9.6
  • Pillow: 10.0.1
from io import BytesIO

from PIL import Image

image = Image.new(mode="RGB", size=(1024, 1024), color="red")

for _ in range(10):
    output = BytesIO()
    image.save(output, format="jpeg")
    output.seek(0)
    print(len(output.read()))

Actual Output:

17013
17014
17014
17014
17014
17013
17013
17014
17014
17014

Expected output:

The number of bytes should be the same for each iteration.

@radarhere
Copy link
Member

How did you install Pillow?

Out of curiosity - how did you find this problem?

@johnnyleitrim
Copy link
Author

@radarhere I installed it with pip install pillow==10.0.1

We found it because we have some integration tests in our product that create JPEGs, and we check the file size of the created JPEG. After upgrading, we saw that we were getting different file sizes for different executions.

@radarhere radarhere added the JPEG label Oct 18, 2023
@radarhere
Copy link
Member

radarhere commented Oct 18, 2023

In our Pillow 10.0.1 wheels, we upgraded from libjpeg-turbo 2.1.5.1 to 3.0.0. Testing, I find that upgrading libjpeg-turbo 3.0.1 fixes this.

So this is resolved by #7472

If you would like to test this, or have an immediate fix, you can install
Pillow-10.2.0.dev0-cp39-cp39-macosx_11_0_arm64.whl.zip. Just unzip it, uninstalling your current Pillow and run python3 -m pip install Pillow-10.2.0.dev0-cp39-cp39-macosx_11_0_arm64.whl

@johnnyleitrim
Copy link
Author

@radarhere Do you know when 10.2.0 will be officially released?

@aclark4life
Copy link
Member

Jan 1?

@hugovk
Copy link
Member

hugovk commented Oct 26, 2023

Close, Jan 2 :)

https://github.com/python-pillow/Pillow/milestone/57

Pillow is released quarterly on January 2nd, April 1st, July 1st and October 15th.

https://pillow.readthedocs.io/en/stable/releasenotes/

@johnnyleitrim
Copy link
Author

From our testing, we can see that Pillow corrupts the last few pixels in the JPEGs, and that's why the file sizes are different. Is there any possibility of an earlier release?

@radarhere
Copy link
Member

Does it help at all if I say that it is not Pillow itself that has the problem, just the wheels of Pillow 10.1.0? So if you install Pillow from source, the problem will go away.

@devbanu
Copy link

devbanu commented Nov 29, 2023

I encountered the same problem in a different way: on macOS the hardware JPEG decoder started complaining when decoding JPEG files created with Pillow 10.1.0.

The error looks like this:
JPEGDecompressSurface : Picture decode failed: e00002e9

@radarhere
Copy link
Member

Pillow 10.2.0 has now been published, with libjpeg-turbo 3.0.1 in the wheels.

@aclark4life
Copy link
Member

Also woo hoo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants