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

Skip to content

Commit 3188f72

Browse files
committed
Merge remote-tracking branch 'msarahan/ImageTutorialNotebookEdit'
Conflicts: doc/users/image_tutorial.rst
2 parents 443afd3 + e2616e8 commit 3188f72

File tree

1 file changed

+96
-107
lines changed

1 file changed

+96
-107
lines changed

doc/users/image_tutorial.rst

Lines changed: 96 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,55 @@ Image tutorial
1010
Startup commands
1111
===================
1212

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:
2426

2527
.. sourcecode:: ipython
2628

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:
3048

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
3450

51+
In [2]: import matplotlib.pyplot as plt
52+
In [3]: import matplotlib.image as mpimg
53+
In [4]: import numpy as np
3554

3655
.. _importing_data:
3756

3857
Importing image data into Numpy arrays
3958
===============================================
4059

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

@@ -61,8 +80,8 @@ And here we go...
6180

6281
.. sourcecode:: ipython
6382

64-
In [4]: img=mpimg.imread('stinkbug.png')
65-
Out[4]:
83+
In [5]: img=mpimg.imread('stinkbug.png')
84+
Out[5]:
6685
array([[[ 0.40784314, 0.40784314, 0.40784314],
6786
[ 0.40784314, 0.40784314, 0.40784314],
6887
[ 0.40784314, 0.40784314, 0.40784314],
@@ -71,39 +90,7 @@ And here we go...
7190
[ 0.42745098, 0.42745098, 0.42745098],
7291
[ 0.42745098, 0.42745098, 0.42745098]],
7392

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-
9093
...,
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-
10794
[[ 0.44313726, 0.44313726, 0.44313726],
10895
[ 0.4509804 , 0.4509804 , 0.4509804 ],
10996
[ 0.4509804 , 0.4509804 , 0.4509804 ],
@@ -145,7 +132,7 @@ plot from the prompt.
145132

146133
.. sourcecode:: ipython
147134

148-
In [5]: imgplot = plt.imshow(img)
135+
In [6]: imgplot = plt.imshow(img)
149136

150137
.. plot::
151138

@@ -155,8 +142,7 @@ plot from the prompt.
155142
img = mpimg.imread('../_static/stinkbug.png')
156143
imgplot = plt.imshow(img)
157144

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

161147
.. _Pseudocolor:
162148

@@ -175,33 +161,31 @@ channel of our data:
175161

176162
.. sourcecode:: ipython
177163

178-
In [6]: lum_img = img[:,:,0]
164+
In [7]: lum_img = img[:,:,0]
179165

180166
This is array slicing. You can read more in the `Numpy tutorial
181167
<http://www.scipy.org/Tentative_NumPy_Tutorial>`_.
182168

183169
.. sourcecode:: ipython
184170

185-
In [7]: imgplot = plt.imshow(lum_img)
171+
In [8]: plt.imshow(lum_img)
186172

187173
.. plot::
188174

189175
import matplotlib.pyplot as plt
190176
import matplotlib.image as mpimg
191177
import numpy as np
192178
img = mpimg.imread('../_static/stinkbug.png')
193-
lum_img = img[:,:,0]
179+
lum_img = img[:, :, 0]
194180
plt.imshow(lum_img)
195181

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,
197183
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.
201185

202186
.. sourcecode:: ipython
203187

204-
In [8]: imgplot.set_cmap('hot')
188+
In [9]: plt.imshow(lum_img, cmap="hot")
205189

206190
.. plot::
207191

@@ -213,20 +197,33 @@ object:
213197
imgplot = plt.imshow(lum_img)
214198
imgplot.set_cmap('hot')
215199

200+
Note that you can also change colormaps on existing plot objects using the
201+
:meth:`~matplotlib.image.Image.set_cmap` method:
202+
216203
.. sourcecode:: ipython
217204

218-
In [9]: imgplot.set_cmap('spectral')
205+
In [10]: imgplot = plt.imshow(lum_img)
206+
In [11]: imgplot.set_cmap('spectral')
219207

220208
.. plot::
221209

222210
import matplotlib.pyplot as plt
223211
import matplotlib.image as mpimg
224212
import numpy as np
225213
img = mpimg.imread('../_static/stinkbug.png')
226-
lum_img = img[:,:,0]
214+
lum_img = img[:, :, 0]
227215
imgplot = plt.imshow(lum_img)
228216
imgplot.set_cmap('spectral')
229217

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+
230227
There are many other colormap schemes available. See the `list and
231228
images of the colormaps
232229
<../examples/color/colormaps_reference.html>`_.
@@ -237,19 +234,20 @@ Color scale reference
237234
------------------------
238235

239236
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.
241238

242239
.. sourcecode:: ipython
243240

244-
In [10]: plt.colorbar()
241+
In [12]: imgplot = plt.imshow(lum_img)
242+
In [13]: plt.colorbar()
245243

246244
.. plot::
247245

248246
import matplotlib.pyplot as plt
249247
import matplotlib.image as mpimg
250248
import numpy as np
251249
img = mpimg.imread('../_static/stinkbug.png')
252-
lum_img = img[:,:,0]
250+
lum_img = img[:, :, 0]
253251
imgplot = plt.imshow(lum_img)
254252
imgplot.set_cmap('spectral')
255253
plt.colorbar()
@@ -272,7 +270,7 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
272270

273271
.. sourcecode:: ipython
274272

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

277275
.. plot::
278276

@@ -281,20 +279,23 @@ image data, we use the :func:`~matplotlib.pyplot.hist` function.
281279
import numpy as np
282280
img = mpimg.imread('../_static/stinkbug.png')
283281
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')
285283

286284
Most often, the "interesting" part of the image is around the peak,
287285
and you can get extra contrast by clipping the regions above and/or
288286
below the peak. In our histogram, it looks like there's not much
289287
useful information in the high end (not many white things in the
290288
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
292291
: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.
294295

295296
.. sourcecode:: ipython
296297

297-
In[11]: imgplot.set_clim(0.0,0.7)
298+
In [15]: imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
298299

299300
.. plot::
300301

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

336+
We'll use the Pillow library that we used to load the image also to resize
337+
the image.
338+
335339
.. sourcecode:: ipython
336340

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

343346
.. plot::
344347

345348
import matplotlib.pyplot as plt
346-
import matplotlib.image as mpimg
347-
import numpy as np
348349
from PIL import Image
349350
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)
354353

355354
Here we have the default interpolation, bilinear, since we did not
356355
give :func:`~matplotlib.pyplot.imshow` any interpolation argument.
@@ -359,37 +358,27 @@ Let's try some others:
359358

360359
.. sourcecode:: ipython
361360

362-
In [10]: imgplot.set_interpolation('nearest')
361+
In [20]: imgplot = plt.imshow(resized, interpolation="nearest")
363362

364363
.. plot::
365364

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

377371
.. sourcecode:: ipython
378372

379-
In [10]: imgplot.set_interpolation('bicubic')
373+
In [21]: imgplot = plt.imshow(resized, interpolation="bicubic")
380374

381375
.. plot::
382376

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

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

0 commit comments

Comments
 (0)