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

Skip to content

Commit cee6fe1

Browse files
committed
added Michael Sarahan's image tutorial from the scipy mpl sprint
svn path=/branches/v0_99_maint/; revision=7582
1 parent 634e98d commit cee6fe1

8 files changed

Lines changed: 339 additions & 10 deletions

File tree

doc/_static/stinkbug.png

106 KB
Loading

doc/_templates/indexsidebar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h3>Need help?</h3>
5252
but it is a good idea to ping us on the mailing list too.</p>
5353

5454
<p>For details on what's new, see the detailed <a href="{{
55-
pathto('_static/CHANGELOG', 1) }}">changelog</a>. Anything that could
55+
pathto('_static/CHANGELOG', 1) }}">changelog</a> or browse the <a href="http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/">source code</a>. Anything that could
5656
require changes to your existing codes is logged in the <a href="{{
5757
pathto('api/api_changes.html', 1) }}">api changes</a> file.</p>
5858

doc/_templates/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{% block rootrellink %}
55
<li><a href="{{ pathto('index') }}">home</a>|&nbsp;</li>
66
<li><a href="{{ pathto('search') }}">search</a>|&nbsp;</li>
7-
<li><a href="examples/index.html">examples</a>|&nbsp;</li>
7+
<li><a href="http://matplotlib.sf.net/examples/index.html">examples</a>|&nbsp;</li>
88
<li><a href="{{ pathto('gallery') }}">gallery</a>|&nbsp;</li>
99
<li><a href="{{ pathto('contents') }}">docs</a> &raquo;</li>
1010
{% endblock %}

doc/devel/coding_guide.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
M.. _coding-guide:
1+
.. _coding-guide:
22

33
************
44
Coding guide

doc/users/image_tutorial.rst

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
.. _image_tutorial:
2+
3+
4+
**************
5+
Image tutorial
6+
**************
7+
8+
.. _imaging_startup:
9+
10+
Startup commands
11+
===================
12+
13+
At the very least, you'll need to have access to the :func:`~matplotlib.pyplot.imshow` function. There are a couple of ways to do it. The easy way for an interactive environment::
14+
15+
$ipython -pylab -wthread
16+
17+
The imshow function is now directly accessible (it's in your
18+
`namespace <http://bytebaker.com/2008/07/30/python-namespaces/>`_).
19+
See also :ref:`pyplot-tutorial`.
20+
21+
The more expressive, easier to understand later method (use this in your scripts to make it easier for others (including your future self) to read)::
22+
23+
$ipython -wthread
24+
25+
.. sourcecode:: ipython
26+
27+
In [1]: import matplotlib.pyplot as plt
28+
In [2]: import matplotlib.image as mpimg
29+
In [3]: import numpy as np
30+
31+
Examples below will use the latter method, for clarity. In these examples, if you use the -pylab method, you can skip the "mpimg." and "plt." prefixes.
32+
33+
.. _importing_data:
34+
35+
Importing image data into Numpy arrays
36+
===============================================
37+
38+
Plotting image data is supported by the Python Image Library (`PIL <http://www.pythonware.com/products/pil/>`_), . Natively, matplotlib only supports PNG images. The commands shown below fall back on PIL if the native read fails.
39+
40+
The image used in this example is a PNG file, but keep that PIL requirement in mind for your own data.
41+
42+
Here's the image we're going to play with:
43+
44+
.. image:: ../_static/stinkbug.png
45+
46+
It's a 24-bit RGB PNG image (8 bits for each of R, G, B). Depending
47+
on where you get your data, the other kinds of image that you'll most
48+
likely encounter are RGBA images, which allow for transparency, or
49+
single-channel grayscale (luminosity) images. You can right click on
50+
it and choose "Save image as" to download it to your computer for the
51+
rest of this tutorial.
52+
53+
And here we go...
54+
55+
.. sourcecode:: ipython
56+
57+
In [4]: img=mpimg.imread('stinkbug.png')
58+
Out[4]:
59+
array([[[ 0.40784314, 0.40784314, 0.40784314],
60+
[ 0.40784314, 0.40784314, 0.40784314],
61+
[ 0.40784314, 0.40784314, 0.40784314],
62+
...,
63+
[ 0.42745098, 0.42745098, 0.42745098],
64+
[ 0.42745098, 0.42745098, 0.42745098],
65+
[ 0.42745098, 0.42745098, 0.42745098]],
66+
67+
[[ 0.41176471, 0.41176471, 0.41176471],
68+
[ 0.41176471, 0.41176471, 0.41176471],
69+
[ 0.41176471, 0.41176471, 0.41176471],
70+
...,
71+
[ 0.42745098, 0.42745098, 0.42745098],
72+
[ 0.42745098, 0.42745098, 0.42745098],
73+
[ 0.42745098, 0.42745098, 0.42745098]],
74+
75+
[[ 0.41960785, 0.41960785, 0.41960785],
76+
[ 0.41568628, 0.41568628, 0.41568628],
77+
[ 0.41568628, 0.41568628, 0.41568628],
78+
...,
79+
[ 0.43137255, 0.43137255, 0.43137255],
80+
[ 0.43137255, 0.43137255, 0.43137255],
81+
[ 0.43137255, 0.43137255, 0.43137255]],
82+
83+
...,
84+
[[ 0.43921569, 0.43921569, 0.43921569],
85+
[ 0.43529412, 0.43529412, 0.43529412],
86+
[ 0.43137255, 0.43137255, 0.43137255],
87+
...,
88+
[ 0.45490196, 0.45490196, 0.45490196],
89+
[ 0.4509804 , 0.4509804 , 0.4509804 ],
90+
[ 0.4509804 , 0.4509804 , 0.4509804 ]],
91+
92+
[[ 0.44313726, 0.44313726, 0.44313726],
93+
[ 0.44313726, 0.44313726, 0.44313726],
94+
[ 0.43921569, 0.43921569, 0.43921569],
95+
...,
96+
[ 0.4509804 , 0.4509804 , 0.4509804 ],
97+
[ 0.44705883, 0.44705883, 0.44705883],
98+
[ 0.44705883, 0.44705883, 0.44705883]],
99+
100+
[[ 0.44313726, 0.44313726, 0.44313726],
101+
[ 0.4509804 , 0.4509804 , 0.4509804 ],
102+
[ 0.4509804 , 0.4509804 , 0.4509804 ],
103+
...,
104+
[ 0.44705883, 0.44705883, 0.44705883],
105+
[ 0.44705883, 0.44705883, 0.44705883],
106+
[ 0.44313726, 0.44313726, 0.44313726]]], dtype=float32)
107+
108+
Note the dtype there - float32. Matplotlib has rescaled the 8 bit data from each channel to floating point data between 0.0 and 1.0. As a side note, the only datatype that PIL can work with is uint8. Matplotlib plotting can handle float32 and uint8, but image reading/writing for any format other than PNG is limited to uint8 data. Why 8 bits? Most displays can only render 8 bits per channel worth of color gradation. Why can they only render 8 bits/channel? Because that's about all the human eye can see. More here (from a photography standpoint): `Luminous Landscape bit depth tutorial <http://www.luminous-landscape.com/tutorials/bit-depth.shtml>`_
109+
110+
Each inner list represents a pixel. Here, with an RGB image, there are 3 values. Since it's a black and white image, R, G, and B are all similar. An RGBA (where A is alpha, or transparency), has 4 values per inner list, and a simple luminance image just has one value (and is thus only a 2-D array, not a 3-D array). For RGB and RGBA images, matplotlib supports float32 and uint8 data types. For grayscale, matplotlib supports only float32. If your array data does not meet one of these descriptions, you need to rescale it.
111+
112+
.. _plotting_data:
113+
114+
Plotting numpy arrays as images
115+
===================================
116+
117+
So, you have your data in a numpy array (either by importing it, or by generating it). Let's render it. In Matplotlib, this is performed using the :func:`~matplotlib.pyplot.imshow` function. Here we'll grab the plot object. This object gives you an easy way to manipulate the plot from the prompt.
118+
119+
.. sourcecode:: ipython
120+
121+
In [5]: imgplot = plt.imshow(img)
122+
123+
.. plot::
124+
125+
import matplotlib.pyplot as plt
126+
import matplotlib.image as mpimg
127+
import numpy as np
128+
img = mpimg.imread('_static/stinkbug.png')
129+
imgplot = plt.imshow(img)
130+
131+
You can also plot any numpy array - just remember that the datatype must be float32 (and range from 0.0 to 1.0) or uint8.
132+
133+
.. _Pseudocolor:
134+
135+
Applying pseudocolor schemes to image plots
136+
-------------------------------------------------
137+
138+
Pseudocolor can be a useful tool for enhancing contrast and visualizing your data more easily. This is especially useful when making presentations of your data using projectors - their contrast is typically quite poor.
139+
140+
Pseudocolor is only relevant to single-channel, grayscale, luminosity images. We currently have an RGB image. Since R, G, and B are all similar (see for yourself above or in your data), we can just pick on channel of our data:
141+
142+
.. sourcecode:: ipython
143+
144+
In [6]: lum_img = img[:,:,0]
145+
146+
This is array slicing. You can read more `here <http://www.scipy.org/Tentative_NumPy_Tutorial>`_
147+
148+
.. sourcecode:: ipython
149+
150+
In [7]: imgplot = mpimg.imshow(lum_img)
151+
152+
.. plot::
153+
154+
import matplotlib.pyplot as plt
155+
import matplotlib.image as mpimg
156+
import numpy as np
157+
img = mpimg.imread('_static/stinkbug.png')
158+
lum_img = img[:,:,0]
159+
plt.imshow(lum_img)
160+
161+
Now, with a luminosity image, the default colormap (aka lookup table, LUT), is applied. The default is called jet. There are plenty of others to choose from. Let's set some others using the :meth:`~matplotlib.image.Image.set_cmap` method on our image plot object:
162+
163+
.. sourcecode:: ipython
164+
165+
In [8]: imgplot.set_cmap('hot')
166+
167+
.. plot::
168+
169+
import matplotlib.pyplot as plt
170+
import matplotlib.image as mpimg
171+
import numpy as np
172+
img = mpimg.imread('_static/stinkbug.png')
173+
lum_img = img[:,:,0]
174+
imgplot = plt.imshow(lum_img)
175+
imgplot.set_cmap('hot')
176+
177+
.. sourcecode:: ipython
178+
179+
In [9]: imgplot.set_cmap('spectral')
180+
181+
.. plot::
182+
183+
import matplotlib.pyplot as plt
184+
import matplotlib.image as mpimg
185+
import numpy as np
186+
img = mpimg.imread('_static/stinkbug.png')
187+
lum_img = img[:,:,0]
188+
imgplot = plt.imshow(lum_img)
189+
imgplot.set_cmap('spectral')
190+
191+
There are many other colormap schemes available. See a list and images of the colormaps `here <http://matplotlib.sourceforge.net/examples/pylab_examples/show_colormaps.html>`_
192+
193+
.. _Color Bars
194+
195+
Color scale reference
196+
------------------------
197+
198+
It's helpful to have an idea of what value a color represents. We can do that by adding color bars. It's as easy as one line:
199+
200+
.. sourcecode:: ipython
201+
In [10]: plt.colorbar()
202+
203+
.. plot::
204+
205+
import matplotlib.pyplot as plt
206+
import matplotlib.image as mpimg
207+
import numpy as np
208+
img = mpimg.imread('_static/stinkbug.png')
209+
lum_img = img[:,:,0]
210+
imgplot = plt.imshow(lum_img)
211+
imgplot.set_cmap('spectral')
212+
plt.colorbar()
213+
214+
This adds a colorbar to your existing figure. This won't automatically change if you change you switch to a different colormap - you have to re-create your plot, and add in the colorbar again.
215+
216+
.. _Data ranges
217+
218+
Examining a specific data range
219+
---------------------------------
220+
221+
Sometimes you want to enhance the contrast in your image, or expand the contrast in a particular region while sacrificing the detail in colors that don't vary much, or don't matter. A good tool to find interesting regions is the histogram. To create a histogram of our image data, we use the :func:`~matplotlib.pyplot.hist` function.
222+
223+
.. sourcecode:: ipython
224+
225+
In[10]: plt.hist(lum_img)
226+
227+
.. plot::
228+
229+
import matplotlib.pyplot as plt
230+
import matplotlib.image as mpimg
231+
import numpy as np
232+
img = mpimg.imread('_static/stinkbug.png')
233+
lum_img = img[:,:,0]
234+
plt.hist(lum_img, range=(0.0,1.0))
235+
236+
Most often, the "interesting" part of the image is around the peak, and you can get extra contrast by clipping the regions above and/or below the peak. In our histogram, it looks like there's not much useful information in the high end (not many white things in the image). Let's adjust the upper limit, so that we effectively "zoom in on" part of the histogram. We do this by calling the :meth:`~matplotlib.image.Image.set_clim` method of the image plot object.
237+
238+
.. sourcecode:: ipython
239+
240+
In[11]: imgplot.set_clim=(0.0,0.7)
241+
242+
.. plot::
243+
244+
import matplotlib.pyplot as plt
245+
import matplotlib.image as mpimg
246+
import numpy as np
247+
fig = plt.figure()
248+
a=fig.add_subplot(1,2,1)
249+
img = mpimg.imread('_static/stinkbug.png')
250+
lum_img = img[:,:,0]
251+
imgplot = plt.imshow(lum_img)
252+
a.set_title('Before')
253+
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation ='horizontal')
254+
a=fig.add_subplot(1,2,2)
255+
imgplot = plt.imshow(lum_img)
256+
imgplot.set_clim(0.0,0.7)
257+
a.set_title('After')
258+
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation='horizontal')
259+
260+
.. _Interpolation:
261+
262+
Array Interpolation schemes
263+
-----------------------------------
264+
Interpolation calculates what the color or value of a pixel "should" be, according to different mathematical schemes. One common place that this happens is when you resize an image. The number of pixels change, but you want the same information. Since pixels are discrete, there's missing space. Interpolation is how you fill that space. This is why your images sometimes come out looking pixelated when you blow them up. The effect is more pronounced when the difference between the original image and the expanded image is greater. Let's take our image and shrink it. We're effectively discarding pixels, only keeping a select few. Now when we plot it, that data gets blown up to the size on your screen. The old pixels aren't there anymore, and the computer has to draw in pixels to fill that space.
265+
266+
.. sourcecode:: ipython
267+
268+
In [8]: import Image
269+
In [9]: img = Image.open('stinkbug.png') # Open image as PIL image object
270+
In [10]: rsize = img.resize((img.size[0]/10,img.size[1]/10)) # Use PIL to resize
271+
In [11]: rsizeArr = np.asarray(rsize) # Get array back
272+
In [12]: imgplot = mpimg.imshow(rsizeArr)
273+
274+
.. plot::
275+
276+
import matplotlib.pyplot as plt
277+
import matplotlib.image as mpimg
278+
import numpy as np
279+
import Image
280+
img = Image.open('_static/stinkbug.png') # opens the file using PIL - it's not an array yet
281+
rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
282+
rsizeArr = np.asarray(rsize)
283+
lum_img = rsizeArr[:,:,0]
284+
imgplot = plt.imshow(rsizeArr)
285+
286+
Here we have the default interpolation, bilinear, since we did not give :func:`~matplotlib.pyplot.imshow` any interpolation argument.
287+
288+
Let's try some others:
289+
290+
.. sourcecode:: ipython
291+
292+
In [10]: imgplot.set_interpolation('nearest')
293+
294+
.. plot::
295+
296+
import matplotlib.pyplot as plt
297+
import matplotlib.image as mpimg
298+
import numpy as np
299+
import Image
300+
img = Image.open('_static/stinkbug.png') # opens the file using PIL - it's not an array yet
301+
rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
302+
rsizeArr = np.asarray(rsize)
303+
lum_img = rsizeArr[:,:,0]
304+
imgplot = plt.imshow(rsizeArr)
305+
imgplot.set_interpolation('nearest')
306+
307+
.. sourcecode:: ipython
308+
309+
In [10]: imgplot.set_interpolation('bicubic')
310+
311+
.. plot::
312+
313+
import matplotlib.pyplot as plt
314+
import matplotlib.image as mpimg
315+
import numpy as np
316+
import Image
317+
img = Image.open('_static/stinkbug.png') # opens the file using PIL - it's not an array yet
318+
rsize = img.resize((img.size[0]/10,img.size[1]/10)) # resize the image
319+
rsizeArr = np.asarray(rsize)
320+
lum_img = rsizeArr[:,:,0]
321+
imgplot = plt.imshow(rsizeArr)
322+
imgplot.set_interpolation('bicubic')
323+
324+
Bicubic interpolation is often used when blowing up photos - people tend to prefer blurry over pixelated.

doc/users/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ User's Guide
1919
customizing.rst
2020
shell.rst
2121
index_text.rst
22+
image_tutorial.rst
2223
artists.rst
2324
legend_guide.rst
2425
event_handling.rst

doc/users/transforms_tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _transformstutorial:
1+
.. _transforms_tutorial:
22

33
**************************
44
Transformations Tutorial

doc/users/whats_new.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ new in matplotlib-0.99
1212

1313
.. _whats-new-mplot3d:
1414

15+
New documentation
16+
-----------------
17+
18+
Jae-Joon Lee has written two new guides :ref:`plotting-guide-legend`
19+
and :ref:`plotting-guide-annotation`. Michael Sarahan has written
20+
:ref:`image_tutorial`. John Hunter has written two new tutorials on
21+
working with paths and transformations: :ref:`path_tutorial` and
22+
:ref:`transforms_tutorial`.
23+
24+
1525
mplot3d
1626
--------
1727

@@ -53,12 +63,6 @@ well as "detach" the spine to offset it away from the data. See
5363
.. plot:: pyplots/whats_new_99_spines.py
5464

5565

56-
New documentation
57-
-----------------
58-
59-
jae-Joon Lee has written two new guides :ref:`plotting-guide-legend`
60-
and :ref:`plotting-guide-annotation`.
61-
6266
.. _whats-new-0-98-4:
6367

6468
new in 0.98.4

0 commit comments

Comments
 (0)