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

Validate self.palette in has_transparency_data() #7796

Conversation

gdesmar
Copy link

@gdesmar gdesmar commented Feb 13, 2024

I have a few files that I was able to open and save in Pillow 9.3.0, that result in an error with the newest (10.2.0) version. I believe that the newer has_transparency_data property can cause an error if the palette is not defined from opening a file.

>>> from PIL import __version__, Image
>>> __version__
'10.2.0'
>>> img = Image.open("icon_6.ico")
>>> img.format
'ICO'
>>> img.height
256
>>> img.width
256
>>> img.save("output", format='WEBP')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python3.12/site-packages/PIL/Image.py", line 2439, in save
    save_handler(self, fp, filename)
  File ".../python3.12/site-packages/PIL/WebPImagePlugin.py", line 338, in _save
    im = im.convert("RGBA" if im.has_transparency_data else "RGB")
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python3.12/site-packages/PIL/Image.py", line 1550, in has_transparency_data
    or (self.mode == "P" and self.palette.mode.endswith("A"))
                             ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'mode'

I believe the solution is as simple as that, but feel free to disregard the PR if it needs a more complex fix.

@radarhere
Copy link
Member

There are two ways of looking at this.

  • One is that the problem is im.has_transparency_data failing to handle the scenario where im.palette is None.
  • The other is that a P mode image shouldn't have a None palette in the first place. P mode images without palettes are really L mode images - see https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes. This is my inclination.

I had a look through our existing test files, and found this happening with some of our TGA images. I've created #7797 to fix that.

Would you be able to upload the images that are causing this for you?

@gdesmar
Copy link
Author

gdesmar commented Feb 13, 2024

I believe the older version of Pillow was assuming no transparency, but I just tested with VSCode and it looks to think that the icons do have transparency. I would like to point out that I often have to work with corrupted files, so hopefully those won't be too much of a problem.
I added the SHA256 hash for the three icons just in case. Here is the non_palette_icons.zip file.

sha256 hash name
e2c5c2c687c9f08a77b307da6f4f5dceaee5fb9a2c099adedac977dec8f29211 icon_1.ico
555ca02331d90dcaeaabc66f5e7932e4882916766d4375288c5b8018c7f3a4b0 icon_2.ico
20fde3f789dfeaffcabd1ae1978e6aee10c977cfdc3e8778af6da1e7b794a50b icon_3.ico

@radarhere
Copy link
Member

Thanks. I've created #7798. That should fix your original error.

@gdesmar
Copy link
Author

gdesmar commented Feb 14, 2024

Thank you for your detailed explanations and quick fixes. :)
I will go ahead and close this PR as I doubt it will be useful.

@gdesmar gdesmar closed this Feb 14, 2024
@radarhere
Copy link
Member

#7798 has now been merged.

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

Successfully merging this pull request may close these issues.

None yet

2 participants