@@ -10,36 +10,55 @@ Image tutorial
10
10
Startup commands
11
11
===================
12
12
13
- In this tutorial we will work interactively with images. To do so we will use
14
- the IPython shell. You can start it with::
15
-
16
- $ipython
17
-
18
- At the very least, you'll need to have access to the
19
- :func: `~matplotlib.pyplot.imshow ` function. The easy way for an interactive
20
- environment: is to use the matplotlib API (see :ref: `artist-tutorial `) where
21
- you use explicit
22
- `namespaces <http://bytebaker.com/2008/07/30/python-namespaces/ >`_ and control
23
- object creation, etc...::
13
+ First, let's start IPython. It is a most excellent enhancement to the
14
+ standard Python prompt, and it ties in especially well with
15
+ Matplotlib. Start IPython either at a shell, or the IPython Notebook now.
16
+
17
+ With IPython started, we now need to connect to a GUI event loop. This
18
+ tells IPython where (and how) to display plots. To connect to a GUI
19
+ loop, execute the **%matplotlib ** magic at your IPython prompt. There's more
20
+ detail on exactly what this does at `IPython's documentation on GUI
21
+ event loops
22
+ <http://ipython.org/ipython-doc/2/interactive/reference.html#gui-event-loop-support> `_.
23
+
24
+ If you're using IPython Notebook, the same commands are available, but
25
+ people commonly use a specific argument to the %matplotlib magic:
24
26
25
27
.. sourcecode :: ipython
26
28
27
- In [1]: import matplotlib.pyplot as plt
28
- In [2]: import matplotlib.image as mpimg
29
- In [3]: import numpy as np
29
+ In [1]: %matplotlib inline
30
+
31
+ This turns on inline plotting, where plot graphics will appear in your
32
+ notebook. This has important implications for interactivity. For inline plotting, commands in
33
+ cells below the cell that outputs a plot will not affect the plot. For example,
34
+ changing the color map is not possible from cells below the cell that creates a plot.
35
+ However, for other backends, such as qt4, that open a separate window,
36
+ cells below those that create the plot will change the plot - it is a
37
+ live object in memory.
38
+
39
+ This tutorial will use matplotlib's imperative-style plotting
40
+ interface, pyplot. This interface maintains global state, and is very
41
+ useful for quickly and easily experimenting with various plot
42
+ settings. The alternative is the object-oriented interface, which is also
43
+ very powerful, and generally more suitable for large application
44
+ development. If you'd like to learn about the object-oriented
45
+ interface, a great place to start is our `FAQ on usage
46
+ <http://matplotlib.org/faq/usage_faq.html> `_. For now, let's get on
47
+ with the imperative-style approach:
30
48
31
- You can now access functions like :func: `~matplotlib.pyplot.imshow ` by using:
32
- `plt.imshow(yourimage) `. You can learn more about these functions in the
33
- :ref: `pyplot-tutorial `.
49
+ .. sourcecode :: ipython
34
50
51
+ In [2]: import matplotlib.pyplot as plt
52
+ In [3]: import matplotlib.image as mpimg
53
+ In [4]: import numpy as np
35
54
36
55
.. _importing_data :
37
56
38
57
Importing image data into Numpy arrays
39
58
===============================================
40
59
41
- Plotting image data is supported by the `Pillow
42
- <http://python-imaging.github.io/> `_) . Natively, matplotlib only
60
+ Loading image data is supported by the `Pillow
61
+ <http://python-imaging.github.io/> `_ library . Natively, matplotlib only
43
62
supports PNG images. The commands shown below fall back on Pillow if the
44
63
native read fails.
45
64
@@ -61,8 +80,8 @@ And here we go...
61
80
62
81
.. sourcecode :: ipython
63
82
64
- In [4 ]: img=mpimg.imread('stinkbug.png')
65
- Out[4 ]:
83
+ In [5 ]: img=mpimg.imread('stinkbug.png')
84
+ Out[5 ]:
66
85
array([[[ 0.40784314, 0.40784314, 0.40784314],
67
86
[ 0.40784314, 0.40784314, 0.40784314],
68
87
[ 0.40784314, 0.40784314, 0.40784314],
@@ -71,39 +90,7 @@ And here we go...
71
90
[ 0.42745098, 0.42745098, 0.42745098],
72
91
[ 0.42745098, 0.42745098, 0.42745098]],
73
92
74
- [[ 0.41176471, 0.41176471, 0.41176471],
75
- [ 0.41176471, 0.41176471, 0.41176471],
76
- [ 0.41176471, 0.41176471, 0.41176471],
77
- ...,
78
- [ 0.42745098, 0.42745098, 0.42745098],
79
- [ 0.42745098, 0.42745098, 0.42745098],
80
- [ 0.42745098, 0.42745098, 0.42745098]],
81
-
82
- [[ 0.41960785, 0.41960785, 0.41960785],
83
- [ 0.41568628, 0.41568628, 0.41568628],
84
- [ 0.41568628, 0.41568628, 0.41568628],
85
- ...,
86
- [ 0.43137255, 0.43137255, 0.43137255],
87
- [ 0.43137255, 0.43137255, 0.43137255],
88
- [ 0.43137255, 0.43137255, 0.43137255]],
89
-
90
93
...,
91
- [[ 0.43921569, 0.43921569, 0.43921569],
92
- [ 0.43529412, 0.43529412, 0.43529412],
93
- [ 0.43137255, 0.43137255, 0.43137255],
94
- ...,
95
- [ 0.45490196, 0.45490196, 0.45490196],
96
- [ 0.4509804 , 0.4509804 , 0.4509804 ],
97
- [ 0.4509804 , 0.4509804 , 0.4509804 ]],
98
-
99
- [[ 0.44313726, 0.44313726, 0.44313726],
100
- [ 0.44313726, 0.44313726, 0.44313726],
101
- [ 0.43921569, 0.43921569, 0.43921569],
102
- ...,
103
- [ 0.4509804 , 0.4509804 , 0.4509804 ],
104
- [ 0.44705883, 0.44705883, 0.44705883],
105
- [ 0.44705883, 0.44705883, 0.44705883]],
106
-
107
94
[[ 0.44313726, 0.44313726, 0.44313726],
108
95
[ 0.4509804 , 0.4509804 , 0.4509804 ],
109
96
[ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -145,7 +132,7 @@ plot from the prompt.
145
132
146
133
.. sourcecode :: ipython
147
134
148
- In [5 ]: imgplot = plt.imshow(img)
135
+ In [6 ]: imgplot = plt.imshow(img)
149
136
150
137
.. plot ::
151
138
@@ -155,8 +142,7 @@ plot from the prompt.
155
142
img = mpimg.imread('../_static/stinkbug.png')
156
143
imgplot = plt.imshow(img)
157
144
158
- You can also plot any numpy array - just remember that the datatype
159
- must be float32 (and range from 0.0 to 1.0) or uint8.
145
+ You can also plot any numpy array.
160
146
161
147
.. _Pseudocolor :
162
148
@@ -175,33 +161,31 @@ channel of our data:
175
161
176
162
.. sourcecode :: ipython
177
163
178
- In [6 ]: lum_img = img[:,:,0]
164
+ In [7 ]: lum_img = img[:,:,0]
179
165
180
166
This is array slicing. You can read more in the `Numpy tutorial
181
167
<http://www.scipy.org/Tentative_NumPy_Tutorial> `_.
182
168
183
169
.. sourcecode :: ipython
184
170
185
- In [7]: imgplot = plt.imshow(lum_img)
171
+ In [8]: plt.imshow(lum_img)
186
172
187
173
.. plot ::
188
174
189
175
import matplotlib.pyplot as plt
190
176
import matplotlib.image as mpimg
191
177
import numpy as np
192
178
img = mpimg.imread('../_static/stinkbug.png')
193
- lum_img = img[:,:, 0]
179
+ lum_img = img[:, :, 0]
194
180
plt.imshow(lum_img)
195
181
196
- Now, with a luminosity image, the default colormap (aka lookup table,
182
+ Now, with a luminosity (2D, no color) image, the default colormap (aka lookup table,
197
183
LUT), is applied. The default is called jet. There are plenty of
198
- others to choose from. Let's set some others using the
199
- :meth: `~matplotlib.image.Image.set_cmap ` method on our image plot
200
- object:
184
+ others to choose from.
201
185
202
186
.. sourcecode :: ipython
203
187
204
- In [8 ]: imgplot.set_cmap(' hot' )
188
+ In [9 ]: plt.imshow(lum_img, cmap=" hot" )
205
189
206
190
.. plot ::
207
191
@@ -213,20 +197,33 @@ object:
213
197
imgplot = plt.imshow(lum_img)
214
198
imgplot.set_cmap('hot')
215
199
200
+ Note that you can also change colormaps on existing plot objects using the
201
+ :meth: `~matplotlib.image.Image.set_cmap ` method:
202
+
216
203
.. sourcecode :: ipython
217
204
218
- In [9]: imgplot.set_cmap('spectral')
205
+ In [10]: imgplot = plt.imshow(lum_img)
206
+ In [11]: imgplot.set_cmap('spectral')
219
207
220
208
.. plot ::
221
209
222
210
import matplotlib.pyplot as plt
223
211
import matplotlib.image as mpimg
224
212
import numpy as np
225
213
img = mpimg.imread('../_static/stinkbug.png')
226
- lum_img = img[:,:, 0]
214
+ lum_img = img[:, :, 0]
227
215
imgplot = plt.imshow(lum_img)
228
216
imgplot.set_cmap('spectral')
229
217
218
+ .. note ::
219
+
220
+ However, remember that in the IPython notebook with the inline backend,
221
+ you can't make changes to plots that have already been rendered. If you
222
+ create imgplot here in one cell, you cannot call set_cmap() on it in a later
223
+ cell and expect the earlier plot to change. Make sure that you enter these
224
+ commands together in one cell. plt commands will not change plots from earlier
225
+ cells.
226
+
230
227
There are many other colormap schemes available. See the `list and
231
228
images of the colormaps
232
229
<../examples/color/colormaps_reference.html> `_.
@@ -237,19 +234,20 @@ Color scale reference
237
234
------------------------
238
235
239
236
It's helpful to have an idea of what value a color represents. We can
240
- do that by adding color bars. It's as easy as one line:
237
+ do that by adding color bars.
241
238
242
239
.. sourcecode :: ipython
243
240
244
- In [10]: plt.colorbar()
241
+ In [12]: imgplot = plt.imshow(lum_img)
242
+ In [13]: plt.colorbar()
245
243
246
244
.. plot ::
247
245
248
246
import matplotlib.pyplot as plt
249
247
import matplotlib.image as mpimg
250
248
import numpy as np
251
249
img = mpimg.imread('../_static/stinkbug.png')
252
- lum_img = img[:,:, 0]
250
+ lum_img = img[:, :, 0]
253
251
imgplot = plt.imshow(lum_img)
254
252
imgplot.set_cmap('spectral')
255
253
plt.colorbar()
@@ -272,7 +270,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
272
270
273
271
.. sourcecode :: ipython
274
272
275
- In[10 ]: plt.hist(lum_img.flatten (), 256, range=(0.0,1.0), fc='k', ec='k')
273
+ In [14 ]: plt.hist(lum_img.ravel (), bins= 256, range=(0.0, 1.0), fc='k', ec='k')
276
274
277
275
.. plot ::
278
276
@@ -281,20 +279,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
281
279
import numpy as np
282
280
img = mpimg.imread('../_static/stinkbug.png')
283
281
lum_img = img[:,:,0]
284
- plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='black ', ec='black ')
282
+ plt.hist(lum_img.flatten(), 256, range=(0.0, 1.0), fc='k ', ec='k ')
285
283
286
284
Most often, the "interesting" part of the image is around the peak,
287
285
and you can get extra contrast by clipping the regions above and/or
288
286
below the peak. In our histogram, it looks like there's not much
289
287
useful information in the high end (not many white things in the
290
288
image). Let's adjust the upper limit, so that we effectively "zoom in
291
- on" part of the histogram. We do this by calling the
289
+ on" part of the histogram. We do this by passing the clim argument to
290
+ imshow. You could also do this by calling the
292
291
:meth: `~matplotlib.image.Image.set_clim ` method of the image plot
293
- object.
292
+ object, but make sure that you do so in the same cell as your plot
293
+ command when working with the IPython Notebook - it will not change
294
+ plots from earlier cells.
294
295
295
296
.. sourcecode :: ipython
296
297
297
- In[11 ]: imgplot.set_clim( 0.0,0.7)
298
+ In [15 ]: imgplot = plt.imshow(lum_img, clim=( 0.0, 0.7) )
298
299
299
300
.. plot ::
300
301
@@ -332,25 +333,23 @@ only keeping a select few. Now when we plot it, that data gets blown
332
333
up to the size on your screen. The old pixels aren't there anymore,
333
334
and the computer has to draw in pixels to fill that space.
334
335
336
+ We'll use the Pillow library that we used to load the image also to resize
337
+ the image.
338
+
335
339
.. sourcecode :: ipython
336
340
337
- In [8]: from PIL import Image
338
- In [9]: img = Image.open('stinkbug.png') # Open image as Pillow image object
339
- In [10]: rsize = img.resize((img.size[0]/10,img.size[1]/10)) # Use Pillow to resize
340
- In [11]: rsizeArr = np.asarray(rsize) # Get array back
341
- In [12]: imgplot = plt.imshow(rsizeArr)
341
+ In [16]: from PIL import Image
342
+ In [17]: img = Image.open('../_static/stinkbug.png')
343
+ In [18]: resized = img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
344
+ In [19]: imgplot = plt.imshow(img)
342
345
343
346
.. plot ::
344
347
345
348
import matplotlib.pyplot as plt
346
- import matplotlib.image as mpimg
347
- import numpy as np
348
349
from PIL import Image
349
350
img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
350
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
351
- rsizeArr = np.asarray(rsize)
352
- lum_img = rsizeArr[:,:,0]
353
- imgplot = plt.imshow(rsizeArr)
351
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
352
+ imgplot = plt.imshow(img)
354
353
355
354
Here we have the default interpolation, bilinear, since we did not
356
355
give :func: `~matplotlib.pyplot.imshow ` any interpolation argument.
@@ -359,37 +358,27 @@ Let's try some others:
359
358
360
359
.. sourcecode :: ipython
361
360
362
- In [10 ]: imgplot.set_interpolation(' nearest' )
361
+ In [20 ]: imgplot = plt.imshow(resized, interpolation=" nearest" )
363
362
364
363
.. plot ::
365
364
366
- import matplotlib.pyplot as plt
367
- import matplotlib.image as mpimg
368
- import numpy as np
369
- from PIL import Image
370
- img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
371
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
372
- rsizeArr = np.asarray(rsize)
373
- lum_img = rsizeArr[:,:,0]
374
- imgplot = plt.imshow(rsizeArr)
375
- imgplot.set_interpolation('nearest')
365
+ import matplotlib.pyplot as plt
366
+ from PIL import Image
367
+ img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
368
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
369
+ imgplot = plt.imshow(img, interpolation="nearest")
376
370
377
371
.. sourcecode :: ipython
378
372
379
- In [10 ]: imgplot.set_interpolation(' bicubic' )
373
+ In [21 ]: imgplot = plt.imshow(resized, interpolation=" bicubic" )
380
374
381
375
.. plot ::
382
376
383
- import matplotlib.pyplot as plt
384
- import matplotlib.image as mpimg
385
- import numpy as np
386
- from PIL import Image
387
- img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
388
- rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
389
- rsizeArr = np.asarray(rsize)
390
- lum_img = rsizeArr[:,:,0]
391
- imgplot = plt.imshow(rsizeArr)
392
- imgplot.set_interpolation('bicubic')
377
+ import matplotlib.pyplot as plt
378
+ from PIL import Image
379
+ img = Image.open('../_static/stinkbug.png') # opens the file using Pillow - it's not an array yet
380
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
381
+ imgplot = plt.imshow(img, interpolation="bicubic")
393
382
394
383
Bicubic interpolation is often used when blowing up photos - people
395
384
tend to prefer blurry over pixelated.
0 commit comments