-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix assertion observed when adding new colors to an indexed-color TIFF #9308
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
base: main
Are you sure you want to change the base?
Conversation
When ImagePalette._new_color_index adds a new color to an indexed-color TIFF, an assertion added in f2302ab could be tripped. The call into Image.histogram could result in ImageFile.load being called, which could eventually replace ImagePalette._palette with a bytes object, which did not satisfy the assertion that it be only bytearray.
58c6303 to
2264418
Compare
|
Hi. Good work on finding this. Just to show the code you're talking about, you're absolutely right, I had expected that Pillow/src/PIL/ImagePalette.py Lines 116 to 120 in ec40c54
would naturally mean that this assertion was valid. Pillow/src/PIL/ImagePalette.py Lines 168 to 169 in ec40c54
However, what I failed to realise was that Pillow/src/PIL/ImagePalette.py Line 133 in ec40c54
which loads the image Line 1681 in ec40c54
which might set the palette data. Lines 901 to 903 in ec40c54
Looking at the setting of the palette bytes there, the data is being sent from our Python layer to C a few lines earlier, Lines 889 to 891 in ec40c54
and then retrieved again. This has the effect of converting it from a rawmode to one of our standardised modes. However, it is now apparent to me that if the rawmode matches the mode, then the Python data doesn't need to be updated - the data returned from our C layer would be identical. I've created #9309 with this idea, and that actually allows your test case to pass. But wait, you say, what if the rawmode is different? Well, it can't be, because Pillow/src/PIL/ImagePalette.py Lines 151 to 153 in ec40c54
So, yes, I think of #9309 as an alternative solution to your problem, with the added benefit of running less code. Let me know what you think. |
|
I've updated one of our existing tests in my PR. See what you think. |
Great! Comments over there, then. → #9309 |
Alternative to #9309
When
ImagePalette._new_color_indexadds a new color to an indexed-color TIFF, an assertion added in f2302ab could be tripped. The call intoImage.histogramcould result inImageFile.loadbeing called, which could eventually replaceImagePalette._palettewith abytesobject, which did not satisfy the assertion that it be onlybytearray.