Skip to content

Correctly load 16-bit images. #622

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,10 +1255,10 @@ def pil_to_array( pilImage ):
grayscale images, the return array is MxN. For RGB images, the
return value is MxNx3. For RGBA images the return value is MxNx4
"""
def toarray(im):
def toarray(im, dtype=np.uint8):
'return a 1D array of floats'
x_str = im.tostring('raw',im.mode,0,-1)
x = np.fromstring(x_str,np.uint8)
x = np.fromstring(x_str, dtype)
return x

if pilImage.mode in ('RGBA', 'RGBX'):
Expand All @@ -1275,6 +1275,11 @@ def toarray(im):
x = toarray(im)
x.shape = im.size[1], im.size[0], 3
return x
elif pilImage.mode=='I;16':
im = pilImage
x = toarray(im, '>u2' if im.mode.endswith('B') else '<u2')
x.shape = im.size[1], im.size[0]
return x

else: # try to convert to an rgba image
try:
Expand Down