Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 694f8bd

Browse files
committed
Added support for PIL images in imshow(), image.py
svn path=/trunk/matplotlib/; revision=603
1 parent 3a301cd commit 694f8bd

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
New entries should be added at the top
22

33
==============================================================
4+
2004-10-19 Added support for PIL images in imshow(), image.py - ADS
5+
46
2004-10-19 Re-worked exception handling in _image.py and _transforms.py
57
to avoid masking problems with shared libraries. - JTM
68

examples/data/lena.jpg

26.8 KB
Loading

examples/image_demo3.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
from matplotlib.matlab import *
3+
import Image
4+
5+
lena = Image.open('data/lena.jpg')
6+
dpi = rcParams['figure.dpi']
7+
figsize = lena.size[0]/dpi, lena.size[1]/dpi
8+
9+
figure(figsize=figsize)
10+
11+
im = imshow(lena, origin='lower', aspect='preserve')
12+
13+
#savefig('image_demo3')
14+
show()
15+

lib/matplotlib/axes.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,13 +1251,15 @@ def imshow(self, X,
12511251
IMSHOW(X, cmap=None, norm=None, aspect=None, interpolation=None,
12521252
alpha=1.0, vmin=None, vmax=None, origin=None, extent=None)
12531253
1254-
IMSHOW(X) - plot image in array X to current axes, resampling to scale
1255-
to axes size
1254+
IMSHOW(X) - plot image X to current axes, resampling to scale to axes
1255+
size (X may be numarray/Numeric array or PIL image)
12561256
12571257
IMSHOW(X, **kwargs) - Use keyword args to control image scaling,
12581258
colormapping etc. See below for details
12591259
1260-
Display the image in float array X; X can have the following shapes
1260+
1261+
Display the image in X to current axes. X may be a float array or a
1262+
PIL image. If X is a float array, X can have the following shapes
12611263
12621264
MxN : luminance (grayscale)
12631265
@@ -1270,7 +1272,7 @@ def imshow(self, X,
12701272
The following kwargs are allowed:
12711273
12721274
* cmap is a cm colormap instance, eg cm.jet. If None, default to rc
1273-
image.cmap value
1275+
image.cmap value (Ignored when X has RGB(A) information)
12741276
12751277
* aspect is one of: free or preserve. if None, default to rc
12761278
image.aspect value
@@ -1280,7 +1282,8 @@ def imshow(self, X,
12801282
If None, default to rc image.interpolation
12811283
12821284
* norm is a matplotlib.colors.normalize instance; default is
1283-
normalization(). This scales luminance -> 0-1.
1285+
normalization(). This scales luminance -> 0-1 (Ignored when X is
1286+
PIL image).
12841287
12851288
* vmin and vmax are used to scale a luminance image to 0-1. If
12861289
either is None, the min and max of the luminance values will be
@@ -1308,8 +1311,7 @@ def imshow(self, X,
13081311
if norm is None:
13091312
im.set_clim(vmin, vmax)
13101313

1311-
1312-
im.set_array(X)
1314+
im.set_data(X)
13131315
im.set_alpha(alpha)
13141316

13151317
xmin, xmax, ymin, ymax = im.get_extent()

lib/matplotlib/image.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from artist import Artist
1010
from colors import normalize, colorConverter
1111
import cm
12+
import numerix
1213
from numerix import arange
1314
import _image
1415

@@ -166,10 +167,16 @@ def write_png(self, fname):
166167
im.write_png(fname)
167168

168169

169-
def set_array(self, A):
170-
'Set the image array from numeric/numarray A'
171-
cm.ScalarMappable.set_array(self, A)
170+
def set_data(self, A):
171+
'Set the image array from numeric/numarray/PIL Image A'
172+
# check if data is PIL Image without importing Image
173+
if hasattr(A,'getpixel'): X = pil_to_array(A)
174+
else: X = A # assume array
175+
cm.ScalarMappable.set_array(self, X)
172176

177+
def set_array(self, A):
178+
'retained for backwards compatibility - use set_data instead'
179+
self.set_data(A)
173180

174181
def get_aspect(self):
175182
"""
@@ -316,3 +323,26 @@ def imread(fname):
316323
return handler(fname)
317324

318325

326+
327+
def pil_to_array( pilImage ):
328+
if pilImage.mode == 'P': # convert from paletted
329+
im = pilImage.convert('RGBX')
330+
else:
331+
im = pilImage
332+
333+
# There's a whole lotta conversion and copying going on
334+
# here -- could it be optimized?
335+
336+
if im.mode in ('RGBA','RGBX'): n_channels = 4
337+
elif im.mode == 'RGB': n_channels = 3
338+
elif im.mode == 'L': n_channels = 1
339+
else: raise RuntimeError('Unknown image mode')
340+
341+
x_str = im.tostring('raw',im.mode,0,-1)
342+
x = numerix.fromstring(x_str,numerix.UInt8)
343+
if n_channels == 1:
344+
x.shape = im.size[1], im.size[0]
345+
else:
346+
x.shape = im.size[1], im.size[0], n_channels
347+
x=x.astype(numerix.Float32)/255.0
348+
return x

0 commit comments

Comments
 (0)