@@ -41,13 +41,21 @@ Examples below will use the latter method, for clarity. In these
41
41
examples, if you use the %pylab method, you can skip the "mpimg." and
42
42
"plt." prefixes.
43
43
44
+ For those of you following along with the IPython Notebook, you'll want to
45
+ pay attention to whether you're using inline plotting or plotting in another
46
+ window. This is also called the "backend." For inline plotting, commands in
47
+ cells below the cell that outputs a plot will not affect the plot. For example,
48
+ changing the color map is not possible from cells below the cell that creates a plot.
49
+ However, for other backends, such as qt4, cells below those that create the plot
50
+ will change the plot - it is a live object in memory.
51
+
44
52
.. _importing_data :
45
53
46
54
Importing image data into Numpy arrays
47
55
===============================================
48
56
49
- Plotting image data is supported by the `Pillow
50
- <http://python-imaging.github.io/> `_) . Natively, matplotlib only
57
+ Loading image data is supported by the `Pillow
58
+ <http://python-imaging.github.io/> `_ library . Natively, matplotlib only
51
59
supports PNG images. The commands shown below fall back on Pillow if the
52
60
native read fails.
53
61
@@ -79,39 +87,7 @@ And here we go...
79
87
[ 0.42745098, 0.42745098, 0.42745098],
80
88
[ 0.42745098, 0.42745098, 0.42745098]],
81
89
82
- [[ 0.41176471, 0.41176471, 0.41176471],
83
- [ 0.41176471, 0.41176471, 0.41176471],
84
- [ 0.41176471, 0.41176471, 0.41176471],
85
- ...,
86
- [ 0.42745098, 0.42745098, 0.42745098],
87
- [ 0.42745098, 0.42745098, 0.42745098],
88
- [ 0.42745098, 0.42745098, 0.42745098]],
89
-
90
- [[ 0.41960785, 0.41960785, 0.41960785],
91
- [ 0.41568628, 0.41568628, 0.41568628],
92
- [ 0.41568628, 0.41568628, 0.41568628],
93
- ...,
94
- [ 0.43137255, 0.43137255, 0.43137255],
95
- [ 0.43137255, 0.43137255, 0.43137255],
96
- [ 0.43137255, 0.43137255, 0.43137255]],
97
-
98
90
...,
99
- [[ 0.43921569, 0.43921569, 0.43921569],
100
- [ 0.43529412, 0.43529412, 0.43529412],
101
- [ 0.43137255, 0.43137255, 0.43137255],
102
- ...,
103
- [ 0.45490196, 0.45490196, 0.45490196],
104
- [ 0.4509804 , 0.4509804 , 0.4509804 ],
105
- [ 0.4509804 , 0.4509804 , 0.4509804 ]],
106
-
107
- [[ 0.44313726, 0.44313726, 0.44313726],
108
- [ 0.44313726, 0.44313726, 0.44313726],
109
- [ 0.43921569, 0.43921569, 0.43921569],
110
- ...,
111
- [ 0.4509804 , 0.4509804 , 0.4509804 ],
112
- [ 0.44705883, 0.44705883, 0.44705883],
113
- [ 0.44705883, 0.44705883, 0.44705883]],
114
-
115
91
[[ 0.44313726, 0.44313726, 0.44313726],
116
92
[ 0.4509804 , 0.4509804 , 0.4509804 ],
117
93
[ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -163,8 +139,7 @@ plot from the prompt.
163
139
img = mpimg.imread('../_static/stinkbug.png')
164
140
imgplot = plt.imshow(img)
165
141
166
- You can also plot any numpy array - just remember that the datatype
167
- must be float32 (and range from 0.0 to 1.0) or uint8.
142
+ You can also plot any numpy array.
168
143
169
144
.. _Pseudocolor :
170
145
@@ -190,26 +165,24 @@ This is array slicing. You can read more in the `Numpy tutorial
190
165
191
166
.. sourcecode :: ipython
192
167
193
- In [7]: imgplot = plt.imshow(lum_img)
168
+ In [7]: plt.imshow(lum_img)
194
169
195
170
.. plot ::
196
171
197
172
import matplotlib.pyplot as plt
198
173
import matplotlib.image as mpimg
199
174
import numpy as np
200
175
img = mpimg.imread('../_static/stinkbug.png')
201
- lum_img = img[:,:, 0]
176
+ lum_img = img[:, :, 0]
202
177
plt.imshow(lum_img)
203
178
204
- Now, with a luminosity image, the default colormap (aka lookup table,
179
+ Now, with a luminosity (2D, no color) image, the default colormap (aka lookup table,
205
180
LUT), is applied. The default is called jet. There are plenty of
206
- others to choose from. Let's set some others using the
207
- :meth: `~matplotlib.image.Image.set_cmap ` method on our image plot
208
- object:
181
+ others to choose from.
209
182
210
183
.. sourcecode :: ipython
211
184
212
- In [8]: imgplot.set_cmap(' hot' )
185
+ In [8]: plt.imshow(lum_img, cmap=" hot" )
213
186
214
187
.. plot ::
215
188
@@ -221,20 +194,33 @@ object:
221
194
imgplot = plt.imshow(lum_img)
222
195
imgplot.set_cmap('hot')
223
196
197
+ Note that you can also change colormaps on existing plot objects using the
198
+ :meth: `~matplotlib.image.Image.set_cmap ` method:
199
+
224
200
.. sourcecode :: ipython
225
201
226
- In [9]: imgplot.set_cmap('spectral')
202
+ In [9]: imgplot = plt.imshow(lum_img)
203
+ In [10]: imgplot.set_cmap('spectral')
227
204
228
205
.. plot ::
229
206
230
207
import matplotlib.pyplot as plt
231
208
import matplotlib.image as mpimg
232
209
import numpy as np
233
210
img = mpimg.imread('../_static/stinkbug.png')
234
- lum_img = img[:,:, 0]
211
+ lum_img = img[:, :, 0]
235
212
imgplot = plt.imshow(lum_img)
236
213
imgplot.set_cmap('spectral')
237
214
215
+ .. note ::
216
+
217
+ However, remember that in the IPython notebook with the inline backend,
218
+ you can't make changes to plots that have already been rendered. If you
219
+ create imgplot here in one cell, you cannot call set_cmap() on it in a later
220
+ cell and expect the earlier plot to change. Make sure that you enter these
221
+ commands together in one cell. plt commands will not change plots from earlier
222
+ cells.
223
+
238
224
There are many other colormap schemes available. See the `list and
239
225
images of the colormaps
240
226
<../examples/color/colormaps_reference.html> `_.
@@ -245,19 +231,20 @@ Color scale reference
245
231
------------------------
246
232
247
233
It's helpful to have an idea of what value a color represents. We can
248
- do that by adding color bars. It's as easy as one line:
234
+ do that by adding color bars.
249
235
250
236
.. sourcecode :: ipython
251
237
252
- In [10]: plt.colorbar()
238
+ In [11]: imgplot = plt.imshow(lum_img)
239
+ In [12]: plt.colorbar()
253
240
254
241
.. plot ::
255
242
256
243
import matplotlib.pyplot as plt
257
244
import matplotlib.image as mpimg
258
245
import numpy as np
259
246
img = mpimg.imread('../_static/stinkbug.png')
260
- lum_img = img[:,:, 0]
247
+ lum_img = img[:, :, 0]
261
248
imgplot = plt.imshow(lum_img)
262
249
imgplot.set_cmap('spectral')
263
250
plt.colorbar()
@@ -280,7 +267,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
280
267
281
268
.. sourcecode :: ipython
282
269
283
- In[10 ]: plt.hist(lum_img.flatten (), 256, range=(0.0,1.0), fc='k', ec='k')
270
+ In [13 ]: plt.hist(lum_img.ravel (), bins= 256, range=(0.0, 1.0), fc='k', ec='k')
284
271
285
272
.. plot ::
286
273
@@ -289,20 +276,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
289
276
import numpy as np
290
277
img = mpimg.imread('../_static/stinkbug.png')
291
278
lum_img = img[:,:,0]
292
- plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='black ', ec='black ')
279
+ plt.hist(lum_img.flatten(), 256, range=(0.0, 1.0), fc='k ', ec='k ')
293
280
294
281
Most often, the "interesting" part of the image is around the peak,
295
282
and you can get extra contrast by clipping the regions above and/or
296
283
below the peak. In our histogram, it looks like there's not much
297
284
useful information in the high end (not many white things in the
298
285
image). Let's adjust the upper limit, so that we effectively "zoom in
299
- on" part of the histogram. We do this by calling the
286
+ on" part of the histogram. We do this by passing the clim argument to
287
+ imshow. You could also do this by calling the
300
288
:meth: `~matplotlib.image.Image.set_clim ` method of the image plot
301
- object.
289
+ object, but make sure that you do so in the same cell as your plot
290
+ command when working with the IPython Notebook - it will not change
291
+ plots from earlier cells.
302
292
303
293
.. sourcecode :: ipython
304
294
305
- In[11 ]: imgplot.set_clim( 0.0,0.7)
295
+ In [14 ]: imgplot = plt.imshow(lum_img, clim=( 0.0, 0.7) )
306
296
307
297
.. plot ::
308
298
@@ -340,25 +330,23 @@ only keeping a select few. Now when we plot it, that data gets blown
340
330
up to the size on your screen. The old pixels aren't there anymore,
341
331
and the computer has to draw in pixels to fill that space.
342
332
333
+ We'll use the Pillow library that we used to load the image also to resize
334
+ the image.
335
+
343
336
.. sourcecode :: ipython
344
337
345
- In [8]: from PIL import Image
346
- In [9]: img = Image.open('stinkbug.png') # Open image as Pillow image object
347
- In [10]: rsize = img.resize((img.size[0]/10,img.size[1]/10)) # Use Pillow to resize
348
- In [11]: rsizeArr = np.asarray(rsize) # Get array back
349
- In [12]: imgplot = plt.imshow(rsizeArr)
338
+ In [15]: import Image
339
+ In [16]: img = Image.open('../_static/stinkbug.png')
340
+ In [17]: resized = img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
341
+ In [18]: imgplot = plt.imshow(img)
350
342
351
343
.. plot ::
352
344
353
345
import matplotlib.pyplot as plt
354
- import matplotlib.image as mpimg
355
- import numpy as np
356
- from PIL import Image
346
+ import Image
357
347
img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
358
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
359
- rsizeArr = np.asarray(rsize)
360
- lum_img = rsizeArr[:,:,0]
361
- imgplot = plt.imshow(rsizeArr)
348
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
349
+ imgplot = plt.imshow(img)
362
350
363
351
Here we have the default interpolation, bilinear, since we did not
364
352
give :func: `~matplotlib.pyplot.imshow ` any interpolation argument.
@@ -367,37 +355,27 @@ Let's try some others:
367
355
368
356
.. sourcecode :: ipython
369
357
370
- In [10 ]: imgplot.set_interpolation(' nearest' )
358
+ In [19 ]: imgplot = plt.imshow(resized, interpolation=" nearest" )
371
359
372
360
.. plot ::
373
361
374
- import matplotlib.pyplot as plt
375
- import matplotlib.image as mpimg
376
- import numpy as np
377
- from PIL import Image
378
- img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
379
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
380
- rsizeArr = np.asarray(rsize)
381
- lum_img = rsizeArr[:,:,0]
382
- imgplot = plt.imshow(rsizeArr)
383
- imgplot.set_interpolation('nearest')
362
+ import matplotlib.pyplot as plt
363
+ import Image
364
+ img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
365
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
366
+ imgplot = plt.imshow(img, interpolation="nearest")
384
367
385
368
.. sourcecode :: ipython
386
369
387
- In [10 ]: imgplot.set_interpolation(' bicubic' )
370
+ In [19 ]: imgplot = plt.imshow(resized, interpolation=" bicubic" )
388
371
389
372
.. plot ::
390
373
391
- import matplotlib.pyplot as plt
392
- import matplotlib.image as mpimg
393
- import numpy as np
394
- from PIL import Image
395
- img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
396
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
397
- rsizeArr = np.asarray(rsize)
398
- lum_img = rsizeArr[:,:,0]
399
- imgplot = plt.imshow(rsizeArr)
400
- imgplot.set_interpolation('bicubic')
374
+ import matplotlib.pyplot as plt
375
+ import Image
376
+ img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
377
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
378
+ imgplot = plt.imshow(img, interpolation="bicubic")
401
379
402
380
Bicubic interpolation is often used when blowing up photos - people
403
381
tend to prefer blurry over pixelated.
0 commit comments