-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Endianness is inconsistent #6769
Comments
Could you provide some more detail about how you worked out this list of endianness behaviour, perhaps some Python code to demonstrate the inconsistency? I understand that 'native' means that the endianness of the environment is used, but what do you mean by 'mode'? |
I worked these out by looking at the code, though it seems I got a few wrong, and some of them have mode checks that I missed. By 'mode' I mean it accepts/returns data in the endianness of the image's mode. from PIL import Image
def t(method, *args, **kwargs):
print(method.__name__ + ' ', end='')
try:
print(method(*args, **kwargs))
except Exception as e:
print(e)
def callMethods(image):
t(x.getcolors)
t(lambda:list(x.getdata()))
t(lambda:list(x.getdata(0)))
t(x.getextrema)
t(x.getpixel,(0,0))
t(x.tobytes)
for mode in ('I;16L','I;16B'):
print(f'\n{mode} New 123')
with Image.new(mode, (1,1), color=123) as x:
callMethods(x)
print(f'\n{mode} From Bytes \\x00{{')
with Image.frombytes(mode, (1,1), data=b'\x00{') as x:
callMethods(x)
print(f'\n{mode} From Bytes {{\\x00')
with Image.frombytes(mode, (1,1), data=b'{\x00') as x:
callMethods(x)
|
Here's a more interesting test: from PIL import Image
def t(method, *args, **kwargs):
print(method.__name__ + ' ', end='')
try:
print(method(*args, **kwargs))
except Exception as e:
print(e)
def callMethods(image):
t(image.getpixel,(0,0))
image.putdata(image.getdata())
t(image.getpixel,(0,0))
for mode in ('I;16L','I;16B'):
print(f'\n{mode} New 123')
with Image.new(mode, (1,1), color=123) as image:
callMethods(image)
print(f'\n{mode} From Bytes \\x00{{')
with Image.frombytes(mode, (1,1), data=b'\x00{') as image:
callMethods(image)
print(f'\n{mode} From Bytes {{\\x00')
with Image.frombytes(mode, (1,1), data=b'{\x00') as image:
callMethods(image)
|
I created a branch with some tests: https://github.com/Yay295/Pillow/tree/bytes_tests There are three failures: That test is just:
Also apparently we can't create BGR images with |
I've created PR #6825. With that, tests from bytes_tests pass, as well as your other code examples earlier. |
I attempted to test the problem with the Would you be able to post code examples of problems apart from |
I added another test to my branch for creating an image with the |
I've created PR #6834 to fix the I;16N problems. |
PR #6825 has now been merged. If I;16N is also fixed, does that resolve this issue? |
I think the Image Stats might be wrong for the I;16 modes as well. And some of the extrema for those and other modes seem to be off by one. I've printed out the stats for the Hopper image for each mode: hopper_image_stats.py.txt For the I;16 modes I believe is due to the histogram calculation not being correct for them. |
The following code from PIL import Image, ImageStat
for mode in ("I;16", "I;16L", "I;16B", "RGB"):
with Image.open("hopper.png") as im:
if mode[:4] == "I;16":
im = im.convert("I").convert(mode)
assert im.mode == mode
print(mode, ImageStat.Stat(im).extrema) gives me
with main and with Pillow 9.3.0. So I don't know what code you used to get |
I was working in Tests/test_imagestat.py, so I used the from PIL import Image, ImageStat
for ext in ("jpg","png","ppm"):
for mode in ("I;16", "I;16L", "I;16B", "RGB"):
with Image.open("hopper."+ext) as im:
if mode[:4] == "I;16":
im = im.convert("I").convert(mode)
assert im.mode == mode
print(mode, ImageStat.Stat(im).extrema)
|
The issue was resolved by #3661, creating a note at https://pillow.readthedocs.io/en/stable/reference/ImageStat.html#PIL.ImageStat.Stat.extrema
|
I've created PR #7010 to add support for creating images with BGR;15, BGR;16 and BGR;24, but to drop support for BGR;32 altogether. |
Image class methods:
Methods that use one of the above methods:
eval
callspoint
fromarray
callsfrombuffer
rotate
callstransform
transform
callsnew
Related: #2228
The text was updated successfully, but these errors were encountered: