@@ -10,44 +10,55 @@ Image tutorial
10
10
Startup commands
11
11
===================
12
12
13
- At the very least, you'll need to have access to the
14
- :func: ` ~matplotlib.pyplot.imshow ` function. There are a couple of
15
- ways to do it. The easy way for an interactive environment::
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
16
17
- $ipython
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> `_.
18
23
19
- to enter the ipython shell, followed by::
24
+ If you're using IPython Notebook, the same commands are available, but
25
+ people commonly use a specific argument to the %matplotlib magic:
20
26
21
- In [1]: %pylab
22
-
23
- to enter the pylab environment.
24
-
25
- The imshow function is now directly accessible (it's in your
26
- `namespace <http://bytebaker.com/2008/07/30/python-namespaces/ >`_).
27
- See also :ref: `pyplot-tutorial `.
27
+ .. sourcecode :: ipython
28
28
29
- The more expressive, easier to understand later method (use this in
30
- your scripts to make it easier for others (including your future self)
31
- to read) is to use the matplotlib API (see :ref: `artist-tutorial `)
32
- where you use explicit namespaces and control object creation, etc...
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:
33
48
34
49
.. sourcecode :: ipython
35
50
36
- In [1]: import matplotlib.pyplot as plt
37
- In [2]: import matplotlib.image as mpimg
38
- In [3]: import numpy as np
39
-
40
- Examples below will use the latter method, for clarity. In these
41
- examples, if you use the %pylab method, you can skip the "mpimg." and
42
- "plt." prefixes.
51
+ In [2]: import matplotlib.pyplot as plt
52
+ In [3]: import matplotlib.image as mpimg
53
+ In [4]: import numpy as np
43
54
44
55
.. _importing_data :
45
56
46
57
Importing image data into Numpy arrays
47
58
===============================================
48
59
49
- Plotting image data is supported by the `Pillow
50
- <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
51
62
supports PNG images. The commands shown below fall back on Pillow if the
52
63
native read fails.
53
64
@@ -69,8 +80,8 @@ And here we go...
69
80
70
81
.. sourcecode :: ipython
71
82
72
- In [4 ]: img=mpimg.imread('stinkbug.png')
73
- Out[4 ]:
83
+ In [5 ]: img=mpimg.imread('stinkbug.png')
84
+ Out[5 ]:
74
85
array([[[ 0.40784314, 0.40784314, 0.40784314],
75
86
[ 0.40784314, 0.40784314, 0.40784314],
76
87
[ 0.40784314, 0.40784314, 0.40784314],
@@ -79,39 +90,7 @@ And here we go...
79
90
[ 0.42745098, 0.42745098, 0.42745098],
80
91
[ 0.42745098, 0.42745098, 0.42745098]],
81
92
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
93
...,
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
94
[[ 0.44313726, 0.44313726, 0.44313726],
116
95
[ 0.4509804 , 0.4509804 , 0.4509804 ],
117
96
[ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -153,7 +132,7 @@ plot from the prompt.
153
132
154
133
.. sourcecode :: ipython
155
134
156
- In [5 ]: imgplot = plt.imshow(img)
135
+ In [6 ]: imgplot = plt.imshow(img)
157
136
158
137
.. plot ::
159
138
@@ -163,8 +142,7 @@ plot from the prompt.
163
142
img = mpimg.imread('../_static/stinkbug.png')
164
143
imgplot = plt.imshow(img)
165
144
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.
145
+ You can also plot any numpy array.
168
146
169
147
.. _Pseudocolor :
170
148
@@ -183,33 +161,31 @@ channel of our data:
183
161
184
162
.. sourcecode :: ipython
185
163
186
- In [6 ]: lum_img = img[:,:,0]
164
+ In [7 ]: lum_img = img[:,:,0]
187
165
188
166
This is array slicing. You can read more in the `Numpy tutorial
189
167
<http://www.scipy.org/Tentative_NumPy_Tutorial> `_.
190
168
191
169
.. sourcecode :: ipython
192
170
193
- In [7]: imgplot = plt.imshow(lum_img)
171
+ In [8]: plt.imshow(lum_img)
194
172
195
173
.. plot ::
196
174
197
175
import matplotlib.pyplot as plt
198
176
import matplotlib.image as mpimg
199
177
import numpy as np
200
178
img = mpimg.imread('../_static/stinkbug.png')
201
- lum_img = img[:,:, 0]
179
+ lum_img = img[:, :, 0]
202
180
plt.imshow(lum_img)
203
181
204
- 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,
205
183
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:
184
+ others to choose from.
209
185
210
186
.. sourcecode :: ipython
211
187
212
- In [8 ]: imgplot.set_cmap(' hot' )
188
+ In [9 ]: plt.imshow(lum_img, cmap=" hot" )
213
189
214
190
.. plot ::
215
191
@@ -221,20 +197,33 @@ object:
221
197
imgplot = plt.imshow(lum_img)
222
198
imgplot.set_cmap('hot')
223
199
200
+ Note that you can also change colormaps on existing plot objects using the
201
+ :meth: `~matplotlib.image.Image.set_cmap ` method:
202
+
224
203
.. sourcecode :: ipython
225
204
226
- In [9]: imgplot.set_cmap('spectral')
205
+ In [10]: imgplot = plt.imshow(lum_img)
206
+ In [11]: imgplot.set_cmap('spectral')
227
207
228
208
.. plot ::
229
209
230
210
import matplotlib.pyplot as plt
231
211
import matplotlib.image as mpimg
232
212
import numpy as np
233
213
img = mpimg.imread('../_static/stinkbug.png')
234
- lum_img = img[:,:, 0]
214
+ lum_img = img[:, :, 0]
235
215
imgplot = plt.imshow(lum_img)
236
216
imgplot.set_cmap('spectral')
237
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
+
238
227
There are many other colormap schemes available. See the `list and
239
228
images of the colormaps
240
229
<../examples/color/colormaps_reference.html> `_.
@@ -245,19 +234,20 @@ Color scale reference
245
234
------------------------
246
235
247
236
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:
237
+ do that by adding color bars.
249
238
250
239
.. sourcecode :: ipython
251
240
252
- In [10]: plt.colorbar()
241
+ In [12]: imgplot = plt.imshow(lum_img)
242
+ In [13]: plt.colorbar()
253
243
254
244
.. plot ::
255
245
256
246
import matplotlib.pyplot as plt
257
247
import matplotlib.image as mpimg
258
248
import numpy as np
259
249
img = mpimg.imread('../_static/stinkbug.png')
260
- lum_img = img[:,:, 0]
250
+ lum_img = img[:, :, 0]
261
251
imgplot = plt.imshow(lum_img)
262
252
imgplot.set_cmap('spectral')
263
253
plt.colorbar()
@@ -280,7 +270,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
280
270
281
271
.. sourcecode :: ipython
282
272
283
- 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')
284
274
285
275
.. plot ::
286
276
@@ -289,20 +279,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
289
279
import numpy as np
290
280
img = mpimg.imread('../_static/stinkbug.png')
291
281
lum_img = img[:,:,0]
292
- 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 ')
293
283
294
284
Most often, the "interesting" part of the image is around the peak,
295
285
and you can get extra contrast by clipping the regions above and/or
296
286
below the peak. In our histogram, it looks like there's not much
297
287
useful information in the high end (not many white things in the
298
288
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
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
300
291
:meth: `~matplotlib.image.Image.set_clim ` method of the image plot
301
- 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.
302
295
303
296
.. sourcecode :: ipython
304
297
305
- In[11 ]: imgplot.set_clim( 0.0,0.7)
298
+ In [15 ]: imgplot = plt.imshow(lum_img, clim=( 0.0, 0.7) )
306
299
307
300
.. plot ::
308
301
@@ -340,25 +333,23 @@ only keeping a select few. Now when we plot it, that data gets blown
340
333
up to the size on your screen. The old pixels aren't there anymore,
341
334
and the computer has to draw in pixels to fill that space.
342
335
336
+ We'll use the Pillow library that we used to load the image also to resize
337
+ the image.
338
+
343
339
.. sourcecode :: ipython
344
340
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)
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)
350
345
351
346
.. plot ::
352
347
353
348
import matplotlib.pyplot as plt
354
- import matplotlib.image as mpimg
355
- import numpy as np
356
349
from PIL import Image
357
350
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)
351
+ img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
352
+ imgplot = plt.imshow(img)
362
353
363
354
Here we have the default interpolation, bilinear, since we did not
364
355
give :func: `~matplotlib.pyplot.imshow ` any interpolation argument.
@@ -367,37 +358,27 @@ Let's try some others:
367
358
368
359
.. sourcecode :: ipython
369
360
370
- In [10 ]: imgplot.set_interpolation(' nearest' )
361
+ In [20 ]: imgplot = plt.imshow(resized, interpolation=" nearest" )
371
362
372
363
.. plot ::
373
364
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')
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")
384
370
385
371
.. sourcecode :: ipython
386
372
387
- In [10 ]: imgplot.set_interpolation(' bicubic' )
373
+ In [21 ]: imgplot = plt.imshow(resized, interpolation=" bicubic" )
388
374
389
375
.. plot ::
390
376
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')
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")
401
382
402
383
Bicubic interpolation is often used when blowing up photos - people
403
384
tend to prefer blurry over pixelated.
0 commit comments