-
-
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
Saving grayscale 16 bit PNG fails #2970
Comments
There's a mapping from Pillow/src/PIL/PngImagePlugin.py Lines 693 to 708 in 7bf5246
It may be possible to add in a set of |
Nice to hear. Also, casting to |
@tazr This seems to work. Is that what you mean by casting to import numpy as np
from PIL import Image
ar = np.ones((32, 32) , dtype=np.uint16)
ar32 = ar.astype(np.uint32)
im = Image.fromarray(ar32) # or more verbose as Image.fromarray(ar32, 'I')
im.save('foo.png') @aclark4life So when/if the enhancement is made tarz's code shall be supported? import numpy as np
from PIL import Image
ar = np.ones((32, 32), dtype=np.uint16)
im = Image.fromarray(ar) # or more verbose as Image.fromarray(ar, 'I;16')
im.save('foo.png') |
Is there any update on this? I tried with the latest pillow version and got an error |
Same issue here. Curiously, the workaround for me involves using Working Exampleimport numpy as np
import imageio
arr = np.full((32, 32), 256, dtype=np.uint16)
imageio.imwrite("~/16-bit_test.png", arr)
img = imageio.imread("~/16-bit_test.png")
print(img.dtype) # => "uint16" and yields:
Apparent BugWhen preparing the example above I noticed that using e.g. import numpy as np
import imageio
arr = np.full((32, 32), 255, dtype=np.uint16)
imageio.imwrite("~/16-bit_test.png", arr)
img = imageio.imread("~/16-bit_test.png")
print(img.dtype) # => "uint8" and:
This is dangerous as e.g. low saturation input images will be falsely represented. I'll do some digging and raise an issue over at the |
Futher to my comment above I have extracted the approach that import numpy as np
from PIL import Image
arr = np.random.randint(0, 2**16-1, (32, 64), dtype=np.uint16) # or np.ones etc.
array_buffer = arr.tobytes()
img = Image.new("I", arr.T.shape)
img.frombytes(array_buffer, 'raw', "I;16")
img.save("16-bit_test_pillow.png") resulting in:
Pillow: 5.1.0 |
I have created PR #3566 to address this. |
What did you do?
Trying to save a grayscale 16 bit PNG.
What did you expect to happen?
Obtain a 16 bit PNG file.
What actually happened?
An error.
What versions of Pillow and Python are you using?
Pillow 5.00
Python 3.5.4
Code:
Output:
The text was updated successfully, but these errors were encountered: