diff --git a/examples/pylab_examples/image_demo3.py b/examples/pylab_examples/image_demo3.py index ba1c7011ae8f..fd27a20380cc 100644 --- a/examples/pylab_examples/image_demo3.py +++ b/examples/pylab_examples/image_demo3.py @@ -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() diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 9b7076795e72..d2d61549709a 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -5,6 +5,7 @@ """ from __future__ import division import os, warnings +import math import numpy as np from numpy import ma @@ -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: @@ -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'): @@ -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, '