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

Changed int conversion in ImagePalette to ord for Python 2 #1593

Closed
wants to merge 2 commits into from

Conversation

radarhere
Copy link
Member

Error reported in #1592

@wiredfool
Copy link
Member

I'm not convinced that this is the right approach yet.

There's got to be something screwy with the palette, because if it was just an bytes/int conversion issue, then we'd likely have run across this before. The code's been in there since the conversion to py3.

What's the character that's throwing it off? How is it encoded in the palette?

@radarhere
Copy link
Member Author

The palette string is -

b'\x00\x01\x002313424635746857968979:8:;9;=:<>;=?<>@=?@>@A?AB@ACACEBDFCEGDFHEGIFHIGIJHJKIJLJLNKMOLNPMOQNPROQSPRTQSTRTUSUVTVWUVXUWYVY[XZ\\Y[]Z\\^[]_\\^`]_a^`b_ac`bdacebdecefdfgeghfhighjgikhjlikmjlnkmolnpmoqnqsprtqsurtvsuwtvxuwyvxzwy{xz|y{}z|~{}\x7f|~\x80}\x7f\x81~x\x8a:\x80\x82\x7f\x81\x83\x80z\x8c<\x82\x84\x81\x83\x85\x82\x84\x86\x83}\x8f?\x85\x87\x84\x86\x88\x85\x87\x89\x86\x88\x8a\x87\x89\x8b\x88\x83\x94D\x8a\x8c\x89\x8b\x8d\x8a\x8c\x8e\x8b\x8d\x8f\x8c\x8e\x90\x8d\x8f\x91\x8e\x90\x92\x8f\x91\x93\x90\x92\x94\x91\x93\x95\x92\x94\x96\x93\x95\x97\x94\x96\x98\x95\x97\x99\x96\x98\x9a\x97\x9a\x9b\x98\x9b\x9d\x99\x9c\x9e\x9b\x9d\x9f\x9c\x9e\xa0\x9d\x9f\xa1\x9e\xa0\xa2\x9f\x9c\xaaf\xa1\xa3\xa0\xa2\xa4\xa1\xa3\xa5\xa2\xa4\xa6\xa3\xa5\xa7\xa4\xa6\xa8\xa5\xa7\xa9\xa6\xa8\xaa\xa7\xa9\xab\xa8\x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e0\xe3\xe5\xe1\xe4\xe6\xe3\xe5\xe7\xe4\xe6\xe8\xe5\xe7\xe9\xe6\xe8\xea\xe7\xe7\xef\xc3\xe9\xeb\xe8\xe8\xf0\xc4\xea\xec\xe9\xeb\xee\xea\xed\xef\xeb\xee\xf0\xed\xef\xf1\xee\xf0\xf2\xef\xf1\xf3\xf0\xf2\xf4\xf1\xf3\xf5\xf2\xf4\xf7\xf3\xf6\xf8\xf4\xf7\xf9\xf6\xf8\xfa\xf7\xf9\xfb\xf8\xfa\xfc\xf9\xfb\xfd\xfa\xfc\xff\xfb\xfe\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'

It looks to me like the '\x00' character is being rendered as '' in Python 2, but converted by int by Python 3.

@wiredfool
Copy link
Member

So, any palette that contains black (#000000) should error out in python 2.x with PIL from 2012 through current master.

@controversial
Copy link

@wiredfool That's in line with what I'm seeing. I'm trying to draw black text and seeing this error on certain images. Since all tests have passed, are you still opposed to merging?

@wiredfool wiredfool modified the milestone: 3.3.0 Jun 11, 2016
@obskyr
Copy link

obskyr commented Jun 16, 2016

A thought just popped into my head after reading the code for the fix. In the original issue, I wrote:

I tried [...] changing int(x) to ord(x) in the source[...]. The image was saved as grayscale, which it wasn't originally.

Does this fix have that problem? Is the image really unmodified, or will the palette still be incidentally converted to grayscale?

The other concern I have is... won't this still break on Python 3? The image in the original issue breaks on Python 3 with the error ValueError: unrecognized raw mode. In an earlier comment, @radarhere mentioned this is due to RGBA;L being an unsupported palette, but it seems to indeed be in the list, and seems to have been so since the initial PIL fork(!).

So what's going on with that? Basically, will this fix 1) not lose palette data for images like the one in the original issue, and 2) make sure images like the one in the original issue work with Python 3 too?

@wiredfool
Copy link
Member

wiredfool commented Jun 16, 2016

I've taken a look at this recently with the various people reporting issues with palette images, and apart from my opinion that the palette code is a type unsafe mess of the first order, we'd probably be better off having our internal representation of a mutable palette be a bytearray rather than a list. It's a simpler conversion from bytes or a list (bytearray(palette)), it's pretty much what we're trying to accomplish, and it's consistent over the pythons that we support.

It looks like tests pass with the minimal changes in https://github.com/wiredfool/Pillow/tree/palette

@obskyr
Copy link

obskyr commented Jun 16, 2016

Okay, so the tests pass. Does the image pass through unharmed?

@wiredfool
Copy link
Member

It appears to.

@radarhere radarhere deleted the ord branch June 25, 2016 19:30
@radarhere
Copy link
Member Author

For the sake of anyone visiting this later, I've replied to the question @obskyr about RGBA;L with #1592 (comment)

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.

4 participants