@@ -10,44 +10,55 @@ Image tutorial
1010Startup commands
1111===================
1212
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.
1616
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> `_.
1823
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:
2026
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
2828
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:
3348
3449.. sourcecode :: ipython
3550
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
4354
4455.. _importing_data :
4556
4657Importing image data into Numpy arrays
4758===============================================
4859
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
5162supports PNG images. The commands shown below fall back on Pillow if the
5263native read fails.
5364
@@ -69,8 +80,8 @@ And here we go...
6980
7081.. sourcecode :: ipython
7182
72- In [4 ]: img=mpimg.imread('stinkbug.png')
73- Out[4 ]:
83+ In [5 ]: img=mpimg.imread('stinkbug.png')
84+ Out[5 ]:
7485 array([[[ 0.40784314, 0.40784314, 0.40784314],
7586 [ 0.40784314, 0.40784314, 0.40784314],
7687 [ 0.40784314, 0.40784314, 0.40784314],
@@ -79,39 +90,7 @@ And here we go...
7990 [ 0.42745098, 0.42745098, 0.42745098],
8091 [ 0.42745098, 0.42745098, 0.42745098]],
8192
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-
9893 ...,
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-
11594 [[ 0.44313726, 0.44313726, 0.44313726],
11695 [ 0.4509804 , 0.4509804 , 0.4509804 ],
11796 [ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -153,7 +132,7 @@ plot from the prompt.
153132
154133.. sourcecode :: ipython
155134
156- In [5 ]: imgplot = plt.imshow(img)
135+ In [6 ]: imgplot = plt.imshow(img)
157136
158137.. plot ::
159138
@@ -163,8 +142,7 @@ plot from the prompt.
163142 img = mpimg.imread('../_static/stinkbug.png')
164143 imgplot = plt.imshow(img)
165144
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.
168146
169147.. _Pseudocolor :
170148
@@ -183,33 +161,31 @@ channel of our data:
183161
184162.. sourcecode :: ipython
185163
186- In [6 ]: lum_img = img[:,:,0]
164+ In [7 ]: lum_img = img[:,:,0]
187165
188166This is array slicing. You can read more in the `Numpy tutorial
189167<http://www.scipy.org/Tentative_NumPy_Tutorial> `_.
190168
191169.. sourcecode :: ipython
192170
193- In [7]: imgplot = plt.imshow(lum_img)
171+ In [8]: plt.imshow(lum_img)
194172
195173.. plot ::
196174
197175 import matplotlib.pyplot as plt
198176 import matplotlib.image as mpimg
199177 import numpy as np
200178 img = mpimg.imread('../_static/stinkbug.png')
201- lum_img = img[:,:, 0]
179+ lum_img = img[:, :, 0]
202180 plt.imshow(lum_img)
203181
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,
205183LUT), 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.
209185
210186.. sourcecode :: ipython
211187
212- In [8 ]: imgplot.set_cmap(' hot' )
188+ In [9 ]: plt.imshow(lum_img, cmap=" hot" )
213189
214190.. plot ::
215191
@@ -221,20 +197,33 @@ object:
221197 imgplot = plt.imshow(lum_img)
222198 imgplot.set_cmap('hot')
223199
200+ Note that you can also change colormaps on existing plot objects using the
201+ :meth: `~matplotlib.image.Image.set_cmap ` method:
202+
224203.. sourcecode :: ipython
225204
226- In [9]: imgplot.set_cmap('spectral')
205+ In [10]: imgplot = plt.imshow(lum_img)
206+ In [11]: imgplot.set_cmap('spectral')
227207
228208.. plot ::
229209
230210 import matplotlib.pyplot as plt
231211 import matplotlib.image as mpimg
232212 import numpy as np
233213 img = mpimg.imread('../_static/stinkbug.png')
234- lum_img = img[:,:, 0]
214+ lum_img = img[:, :, 0]
235215 imgplot = plt.imshow(lum_img)
236216 imgplot.set_cmap('spectral')
237217
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+
238227There are many other colormap schemes available. See the `list and
239228images of the colormaps
240229<../examples/color/colormaps_reference.html> `_.
@@ -245,19 +234,20 @@ Color scale reference
245234------------------------
246235
247236It'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.
249238
250239.. sourcecode :: ipython
251240
252- In [10]: plt.colorbar()
241+ In [12]: imgplot = plt.imshow(lum_img)
242+ In [13]: plt.colorbar()
253243
254244.. plot ::
255245
256246 import matplotlib.pyplot as plt
257247 import matplotlib.image as mpimg
258248 import numpy as np
259249 img = mpimg.imread('../_static/stinkbug.png')
260- lum_img = img[:,:, 0]
250+ lum_img = img[:, :, 0]
261251 imgplot = plt.imshow(lum_img)
262252 imgplot.set_cmap('spectral')
263253 plt.colorbar()
@@ -280,7 +270,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
280270
281271.. sourcecode :: ipython
282272
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')
284274
285275.. plot ::
286276
@@ -289,20 +279,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
289279 import numpy as np
290280 img = mpimg.imread('../_static/stinkbug.png')
291281 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 ')
293283
294284Most often, the "interesting" part of the image is around the peak,
295285and you can get extra contrast by clipping the regions above and/or
296286below the peak. In our histogram, it looks like there's not much
297287useful information in the high end (not many white things in the
298288image). 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
300291: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.
302295
303296.. sourcecode :: ipython
304297
305- In[11 ]: imgplot.set_clim( 0.0,0.7)
298+ In [15 ]: imgplot = plt.imshow(lum_img, clim=( 0.0, 0.7) )
306299
307300.. plot ::
308301
@@ -340,25 +333,23 @@ only keeping a select few. Now when we plot it, that data gets blown
340333up to the size on your screen. The old pixels aren't there anymore,
341334and the computer has to draw in pixels to fill that space.
342335
336+ We'll use the Pillow library that we used to load the image also to resize
337+ the image.
338+
343339.. sourcecode :: ipython
344340
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)
350345
351346.. plot ::
352347
353348 import matplotlib.pyplot as plt
354- import matplotlib.image as mpimg
355- import numpy as np
356349 from PIL import Image
357350 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)
362353
363354Here we have the default interpolation, bilinear, since we did not
364355give :func: `~matplotlib.pyplot.imshow ` any interpolation argument.
@@ -367,37 +358,27 @@ Let's try some others:
367358
368359.. sourcecode :: ipython
369360
370- In [10 ]: imgplot.set_interpolation(' nearest' )
361+ In [20 ]: imgplot = plt.imshow(resized, interpolation=" nearest" )
371362
372363.. plot ::
373364
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")
384370
385371.. sourcecode :: ipython
386372
387- In [10 ]: imgplot.set_interpolation(' bicubic' )
373+ In [21 ]: imgplot = plt.imshow(resized, interpolation=" bicubic" )
388374
389375.. plot ::
390376
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")
401382
402383Bicubic interpolation is often used when blowing up photos - people
403384tend to prefer blurry over pixelated.
0 commit comments