Skip to content

Fix imread issues mentioned on matplotlib-devel #616

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 8 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/pylab_examples/image_demo3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
figure(figsize=figsize)
ax = axes([0,0,1,1], frameon=False)
ax.set_axis_off()
im = imshow(lena, origin='lower')
im = imshow(lena)

show()

24 changes: 18 additions & 6 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from __future__ import division
import os, warnings
import math

import numpy as np
from numpy import ma
Expand Down Expand Up @@ -1189,6 +1190,9 @@ def pilread():
if cbook.is_string_like(fname):
basename, ext = os.path.splitext(fname)
ext = ext.lower()[1:]
elif hasattr(fname, 'name'):
basename, ext = os.path.splitext(fname.name)
ext = ext.lower()[1:]
else:
ext = 'png'
else:
Expand Down Expand Up @@ -1255,14 +1259,14 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,

def pil_to_array( pilImage ):
"""
load a PIL image and return it as a numpy array of uint8. For
load a PIL image and return it as a numpy array. For
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):
'return a 1D array of floats'
x_str = im.tostring('raw',im.mode,0,-1)
x = np.fromstring(x_str,np.uint8)
def toarray(im, dtype=np.uint8):
'return a 1D array of dtype'
x_str = im.tostring('raw', im.mode)
x = np.fromstring(x_str, dtype)
return x

if pilImage.mode in ('RGBA', 'RGBX'):
Expand All @@ -1279,7 +1283,15 @@ def toarray(im):
x = toarray(im)
x.shape = im.size[1], im.size[0], 3
return x

elif pilImage.mode.startswith('I;16'):
# return MxN luminance array of uint16
im = pilImage
if im.mode.endswith('B'):
x = toarray(im, '>u2')
else:
x = toarray(im, '<u2')
x.shape = im.size[1], im.size[0]
return x.astype('=u2')
else: # try to convert to an rgba image
try:
im = pilImage.convert('RGBA')
Expand Down
Binary file not shown.
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def test_image_python_io():
buffer.seek(0)
plt.imread(buffer)

def test_imread_pil_uint16():
img = plt.imread(os.path.join(os.path.dirname(__file__),
'baseline_images/test_image/uint16.tif'))
assert (img.dtype == np.uint16)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be turned into an image comparison test to ensure that the fields of the image are correctly read in, endianness works etc. Or if that's not practical, maybe assert about a few of the data values?

# def test_image_unicode_io():
# fig = plt.figure()
# ax = fig.add_subplot(111)
Expand Down