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

Skip to content

Commit b0bbeae

Browse files
committed
DOC: image tutorial touch-up
- expand some text regarding backend usage and IPython notebook plot - improve style a bit (PEP 8) - shorten array printout - convert image resizing example to use PIL
1 parent a43a2f9 commit b0bbeae

File tree

1 file changed

+65
-87
lines changed

1 file changed

+65
-87
lines changed

doc/users/image_tutorial.rst

Lines changed: 65 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ Examples below will use the latter method, for clarity. In these
4141
examples, if you use the %pylab method, you can skip the "mpimg." and
4242
"plt." prefixes.
4343

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+
4452
.. _importing_data:
4553

4654
Importing image data into Numpy arrays
4755
===============================================
4856

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
5159
supports PNG images. The commands shown below fall back on Pillow if the
5260
native read fails.
5361

@@ -79,39 +87,7 @@ And here we go...
7987
[ 0.42745098, 0.42745098, 0.42745098],
8088
[ 0.42745098, 0.42745098, 0.42745098]],
8189

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-
9890
...,
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-
11591
[[ 0.44313726, 0.44313726, 0.44313726],
11692
[ 0.4509804 , 0.4509804 , 0.4509804 ],
11793
[ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -163,8 +139,7 @@ plot from the prompt.
163139
img = mpimg.imread('../_static/stinkbug.png')
164140
imgplot = plt.imshow(img)
165141

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.
168143

169144
.. _Pseudocolor:
170145

@@ -190,26 +165,24 @@ This is array slicing. You can read more in the `Numpy tutorial
190165

191166
.. sourcecode:: ipython
192167

193-
In [7]: imgplot = plt.imshow(lum_img)
168+
In [7]: plt.imshow(lum_img)
194169

195170
.. plot::
196171

197172
import matplotlib.pyplot as plt
198173
import matplotlib.image as mpimg
199174
import numpy as np
200175
img = mpimg.imread('../_static/stinkbug.png')
201-
lum_img = img[:,:,0]
176+
lum_img = img[:, :, 0]
202177
plt.imshow(lum_img)
203178

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,
205180
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.
209182

210183
.. sourcecode:: ipython
211184

212-
In [8]: imgplot.set_cmap('hot')
185+
In [8]: plt.imshow(lum_img, cmap="hot")
213186

214187
.. plot::
215188

@@ -221,20 +194,33 @@ object:
221194
imgplot = plt.imshow(lum_img)
222195
imgplot.set_cmap('hot')
223196

197+
Note that you can also change colormaps on existing plot objects using the
198+
:meth:`~matplotlib.image.Image.set_cmap` method:
199+
224200
.. sourcecode:: ipython
225201

226-
In [9]: imgplot.set_cmap('spectral')
202+
In [9]: imgplot = plt.imshow(lum_img)
203+
In [10]: imgplot.set_cmap('spectral')
227204

228205
.. plot::
229206

230207
import matplotlib.pyplot as plt
231208
import matplotlib.image as mpimg
232209
import numpy as np
233210
img = mpimg.imread('../_static/stinkbug.png')
234-
lum_img = img[:,:,0]
211+
lum_img = img[:, :, 0]
235212
imgplot = plt.imshow(lum_img)
236213
imgplot.set_cmap('spectral')
237214

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+
238224
There are many other colormap schemes available. See the `list and
239225
images of the colormaps
240226
<../examples/color/colormaps_reference.html>`_.
@@ -245,19 +231,20 @@ Color scale reference
245231
------------------------
246232

247233
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.
249235

250236
.. sourcecode:: ipython
251237

252-
In [10]: plt.colorbar()
238+
In [11]: imgplot = plt.imshow(lum_img)
239+
In [12]: plt.colorbar()
253240

254241
.. plot::
255242

256243
import matplotlib.pyplot as plt
257244
import matplotlib.image as mpimg
258245
import numpy as np
259246
img = mpimg.imread('../_static/stinkbug.png')
260-
lum_img = img[:,:,0]
247+
lum_img = img[:, :, 0]
261248
imgplot = plt.imshow(lum_img)
262249
imgplot.set_cmap('spectral')
263250
plt.colorbar()
@@ -280,7 +267,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
280267

281268
.. sourcecode:: ipython
282269

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')
284271

285272
.. plot::
286273

@@ -289,20 +276,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
289276
import numpy as np
290277
img = mpimg.imread('../_static/stinkbug.png')
291278
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')
293280

294281
Most often, the "interesting" part of the image is around the peak,
295282
and you can get extra contrast by clipping the regions above and/or
296283
below the peak. In our histogram, it looks like there's not much
297284
useful information in the high end (not many white things in the
298285
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
300288
: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.
302292

303293
.. sourcecode:: ipython
304294

305-
In[11]: imgplot.set_clim(0.0,0.7)
295+
In [14]: imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
306296

307297
.. plot::
308298

@@ -340,25 +330,23 @@ only keeping a select few. Now when we plot it, that data gets blown
340330
up to the size on your screen. The old pixels aren't there anymore,
341331
and the computer has to draw in pixels to fill that space.
342332

333+
We'll use the Pillow library that we used to load the image also to resize
334+
the image.
335+
343336
.. sourcecode:: ipython
344337

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)
350342

351343
.. plot::
352344

353345
import matplotlib.pyplot as plt
354-
import matplotlib.image as mpimg
355-
import numpy as np
356-
from PIL import Image
346+
import Image
357347
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)
362350

363351
Here we have the default interpolation, bilinear, since we did not
364352
give :func:`~matplotlib.pyplot.imshow` any interpolation argument.
@@ -367,37 +355,27 @@ Let's try some others:
367355

368356
.. sourcecode:: ipython
369357

370-
In [10]: imgplot.set_interpolation('nearest')
358+
In [19]: imgplot = plt.imshow(resized, interpolation="nearest")
371359

372360
.. plot::
373361

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")
384367

385368
.. sourcecode:: ipython
386369

387-
In [10]: imgplot.set_interpolation('bicubic')
370+
In [19]: imgplot = plt.imshow(resized, interpolation="bicubic")
388371

389372
.. plot::
390373

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")
401379

402380
Bicubic interpolation is often used when blowing up photos - people
403381
tend to prefer blurry over pixelated.

0 commit comments

Comments
 (0)