forked from matplotlib/matplotlib.github.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_tutorial.html
More file actions
433 lines (411 loc) · 28.4 KB
/
image_tutorial.html
File metadata and controls
433 lines (411 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Image tutorial — Matplotlib 1.3.1 documentation</title>
<link rel="stylesheet" href="../_static/mpl.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.3.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Matplotlib 1.3.1 documentation"
href="../_static/opensearch.xml"/>
<link rel="top" title="Matplotlib 1.3.1 documentation" href="../index.html" />
<link rel="up" title="User’s Guide" href="index.html" />
<link rel="next" title="Artist tutorial" href="artists.html" />
<link rel="prev" title="Annotating text" href="annotations_intro.html" />
</head>
<body>
<!-- Piwik -->
<script type="text/javascript">
if ("matplotlib.sourceforge.net" == document.location.hostname ||
"matplotlib.sf.net" == document.location.hostname) {
var pkBaseURL = (("https:" == document.location.protocol) ? "https://apps.sourceforge.net/piwik/matplotlib/" : "http://apps.sourceforge.net/piwik/matplotlib/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
<script type="text/javascript">
if ("matplotlib.sourceforge.net" == document.location.hostname ||
"matplotlib.sf.net" == document.location.hostname) {
piwik_action_name = '';
piwik_idsite = 1;
piwik_url = pkBaseURL + "piwik.php";
piwik_log(piwik_action_name, piwik_idsite, piwik_url);
document.write(unescape('%3Cobject%3E%3Cnoscript%3E%3Cp%3E%3Cimg src="http://apps.sourceforge.net/piwik/matplotlib/piwik.php?idsite=1" alt="piwik"/%3E%3C/p%3E%3C/noscript%3E%3C/object%3E'));
}
</script>
<!-- End Piwik Tag -->
<link rel="shortcut icon" href="_static/favicon.ico">
<!-- The "Fork me on github" ribbon -->
<img style="float: right; margin-bottom: -40px; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" usemap="#ribbonmap"/>
<map name="ribbonmap">
<area shape="poly" coords="15,0,148,-1,148,135" href="https://github.com/matplotlib/matplotlib" title="Fork me on GitHub" />
</map>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="../index.html"><img src="../_static/logo2.png" border="0" alt="matplotlib"/></a>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="artists.html" title="Artist tutorial"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="annotations_intro.html" title="Annotating text"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">home</a>| </li>
<li><a href="../examples/index.html">examples</a>| </li>
<li><a href="../gallery.html">gallery</a>| </li>
<li><a href="../api/pyplot_summary.html">pyplot</a>| </li>
<li><a href="../contents.html">docs</a> »</li>
<li><a href="index.html" accesskey="U">User’s Guide</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Image tutorial</a><ul>
<li><a class="reference internal" href="#startup-commands">Startup commands</a></li>
<li><a class="reference internal" href="#importing-image-data-into-numpy-arrays">Importing image data into Numpy arrays</a></li>
<li><a class="reference internal" href="#plotting-numpy-arrays-as-images">Plotting numpy arrays as images</a><ul>
<li><a class="reference internal" href="#applying-pseudocolor-schemes-to-image-plots">Applying pseudocolor schemes to image plots</a></li>
<li><a class="reference internal" href="#color-scale-reference">Color scale reference</a></li>
<li><a class="reference internal" href="#examining-a-specific-data-range">Examining a specific data range</a></li>
<li><a class="reference internal" href="#array-interpolation-schemes">Array Interpolation schemes</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="annotations_intro.html"
title="previous chapter">Annotating text</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="artists.html"
title="next chapter">Artist tutorial</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/users/image_tutorial.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="image-tutorial">
<span id="id1"></span><h1>Image tutorial<a class="headerlink" href="#image-tutorial" title="Permalink to this headline">¶</a></h1>
<div class="section" id="startup-commands">
<span id="imaging-startup"></span><h2>Startup commands<a class="headerlink" href="#startup-commands" title="Permalink to this headline">¶</a></h2>
<p>At the very least, you’ll need to have access to the
<a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><tt class="xref py py-func docutils literal"><span class="pre">imshow()</span></tt></a> function. There are a couple of
ways to do it. The easy way for an interactive environment:</p>
<div class="highlight-python"><pre>$ipython -pylab</pre>
</div>
<p>The imshow function is now directly accessible (it’s in your
<a class="reference external" href="http://bytebaker.com/2008/07/30/python-namespaces/">namespace</a>).
See also <a class="reference internal" href="pyplot_tutorial.html#pyplot-tutorial"><em>Pyplot tutorial</em></a>.</p>
<p>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) is to use the matplotlib API (see <a class="reference internal" href="artists.html#artist-tutorial"><em>Artist tutorial</em></a>)
where you use explicit namespaces and control object creation, etc...</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="gp">In [2]: </span><span class="kn">import</span> <span class="nn">matplotlib.image</span> <span class="kn">as</span> <span class="nn">mpimg</span>
<span class="gp">In [3]: </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>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.</p>
</div>
<div class="section" id="importing-image-data-into-numpy-arrays">
<span id="importing-data"></span><h2>Importing image data into Numpy arrays<a class="headerlink" href="#importing-image-data-into-numpy-arrays" title="Permalink to this headline">¶</a></h2>
<p>Plotting image data is supported by the Python Image Library (<a class="reference external" href="http://www.pythonware.com/products/pil/">PIL</a>). Natively, matplotlib
only supports PNG images. The commands shown below fall back on PIL
if the native read fails.</p>
<p>The image used in this example is a PNG file, but keep that PIL
requirement in mind for your own data.</p>
<p>Here’s the image we’re going to play with:</p>
<img alt="../_images/stinkbug.png" src="../_images/stinkbug.png" />
<p>It’s a 24-bit RGB PNG image (8 bits for each of R, G, B). Depending
on where you get your data, the other kinds of image that you’ll most
likely encounter are RGBA images, which allow for transparency, or
single-channel grayscale (luminosity) images. You can right click on
it and choose “Save image as” to download it to your computer for the
rest of this tutorial.</p>
<p>And here we go...</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [4]: </span><span class="n">img</span><span class="o">=</span><span class="n">mpimg</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="s">'stinkbug.png'</span><span class="p">)</span>
<span class="go">Out[4]:</span>
<span class="go">array([[[ 0.40784314, 0.40784314, 0.40784314],</span>
<span class="go"> [ 0.40784314, 0.40784314, 0.40784314],</span>
<span class="go"> [ 0.40784314, 0.40784314, 0.40784314],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098],</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098],</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098]],</span>
<span class="go"> [[ 0.41176471, 0.41176471, 0.41176471],</span>
<span class="go"> [ 0.41176471, 0.41176471, 0.41176471],</span>
<span class="go"> [ 0.41176471, 0.41176471, 0.41176471],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098],</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098],</span>
<span class="go"> [ 0.42745098, 0.42745098, 0.42745098]],</span>
<span class="go"> [[ 0.41960785, 0.41960785, 0.41960785],</span>
<span class="go"> [ 0.41568628, 0.41568628, 0.41568628],</span>
<span class="go"> [ 0.41568628, 0.41568628, 0.41568628],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.43137255, 0.43137255, 0.43137255],</span>
<span class="go"> [ 0.43137255, 0.43137255, 0.43137255],</span>
<span class="go"> [ 0.43137255, 0.43137255, 0.43137255]],</span>
<span class="go"> ...,</span>
<span class="go"> [[ 0.43921569, 0.43921569, 0.43921569],</span>
<span class="go"> [ 0.43529412, 0.43529412, 0.43529412],</span>
<span class="go"> [ 0.43137255, 0.43137255, 0.43137255],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.45490196, 0.45490196, 0.45490196],</span>
<span class="go"> [ 0.4509804 , 0.4509804 , 0.4509804 ],</span>
<span class="go"> [ 0.4509804 , 0.4509804 , 0.4509804 ]],</span>
<span class="go"> [[ 0.44313726, 0.44313726, 0.44313726],</span>
<span class="go"> [ 0.44313726, 0.44313726, 0.44313726],</span>
<span class="go"> [ 0.43921569, 0.43921569, 0.43921569],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.4509804 , 0.4509804 , 0.4509804 ],</span>
<span class="go"> [ 0.44705883, 0.44705883, 0.44705883],</span>
<span class="go"> [ 0.44705883, 0.44705883, 0.44705883]],</span>
<span class="go"> [[ 0.44313726, 0.44313726, 0.44313726],</span>
<span class="go"> [ 0.4509804 , 0.4509804 , 0.4509804 ],</span>
<span class="go"> [ 0.4509804 , 0.4509804 , 0.4509804 ],</span>
<span class="go"> ...,</span>
<span class="go"> [ 0.44705883, 0.44705883, 0.44705883],</span>
<span class="go"> [ 0.44705883, 0.44705883, 0.44705883],</span>
<span class="go"> [ 0.44313726, 0.44313726, 0.44313726]]], dtype=float32)</span>
</pre></div>
</div>
<p>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): <a class="reference external" href="http://www.luminous-landscape.com/tutorials/bit-depth.shtml">Luminous Landscape bit depth tutorial</a>.</p>
<p>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.</p>
</div>
<div class="section" id="plotting-numpy-arrays-as-images">
<span id="plotting-data"></span><h2>Plotting numpy arrays as images<a class="headerlink" href="#plotting-numpy-arrays-as-images" title="Permalink to this headline">¶</a></h2>
<p>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 <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><tt class="xref py py-func docutils literal"><span class="pre">imshow()</span></tt></a> function. Here we’ll grab
the plot object. This object gives you an easy way to manipulate the
plot from the prompt.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [5]: </span><span class="n">imgplot</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-1.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-1.png">png</a>, <a class="reference external" href="../users/image_tutorial-1.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-1.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-1.png" src="../_images/image_tutorial-1.png" />
</div>
<p>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.</p>
<div class="section" id="applying-pseudocolor-schemes-to-image-plots">
<span id="pseudocolor"></span><h3>Applying pseudocolor schemes to image plots<a class="headerlink" href="#applying-pseudocolor-schemes-to-image-plots" title="Permalink to this headline">¶</a></h3>
<p>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.</p>
<p>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 one
channel of our data:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [6]: </span><span class="n">lum_img</span> <span class="o">=</span> <span class="n">img</span><span class="p">[:,:,</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<p>This is array slicing. You can read more in the <a class="reference external" href="http://www.scipy.org/Tentative_NumPy_Tutorial">Numpy tutorial</a>.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [7]: </span><span class="n">imgplot</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">lum_img</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-2.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-2.png">png</a>, <a class="reference external" href="../users/image_tutorial-2.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-2.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-2.png" src="../_images/image_tutorial-2.png" />
</div>
<p>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
<tt class="xref py py-meth docutils literal"><span class="pre">set_cmap()</span></tt> method on our image plot
object:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [8]: </span><span class="n">imgplot</span><span class="o">.</span><span class="n">set_cmap</span><span class="p">(</span><span class="s">'hot'</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-3.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-3.png">png</a>, <a class="reference external" href="../users/image_tutorial-3.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-3.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-3.png" src="../_images/image_tutorial-3.png" />
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [9]: </span><span class="n">imgplot</span><span class="o">.</span><span class="n">set_cmap</span><span class="p">(</span><span class="s">'spectral'</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-4.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-4.png">png</a>, <a class="reference external" href="../users/image_tutorial-4.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-4.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-4.png" src="../_images/image_tutorial-4.png" />
</div>
<p>There are many other colormap schemes available. See the <a class="reference external" href="http://matplotlib.org/examples/color/colormaps_reference.html">list and
images of the colormaps</a>.</p>
</div>
<div class="section" id="color-scale-reference">
<span id="color-bars"></span><h3>Color scale reference<a class="headerlink" href="#color-scale-reference" title="Permalink to this headline">¶</a></h3>
<p>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:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [10]: </span><span class="n">plt</span><span class="o">.</span><span class="n">colorbar</span><span class="p">()</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-5.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-5.png">png</a>, <a class="reference external" href="../users/image_tutorial-5.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-5.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-5.png" src="../_images/image_tutorial-5.png" />
</div>
<p>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.</p>
</div>
<div class="section" id="examining-a-specific-data-range">
<span id="data-ranges"></span><h3>Examining a specific data range<a class="headerlink" href="#examining-a-specific-data-range" title="Permalink to this headline">¶</a></h3>
<p>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 <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.hist" title="matplotlib.pyplot.hist"><tt class="xref py py-func docutils literal"><span class="pre">hist()</span></tt></a> function.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="go">In[10]: plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='k', ec='k')</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-6.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-6.png">png</a>, <a class="reference external" href="../users/image_tutorial-6.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-6.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-6.png" src="../_images/image_tutorial-6.png" />
</div>
<p>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
<tt class="xref py py-meth docutils literal"><span class="pre">set_clim()</span></tt> method of the image plot
object.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="go">In[11]: imgplot.set_clim(0.0,0.7)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-7.py">Source code</a>, <a class="reference external" href="../users/image_tutorial-7.png">png</a>, <a class="reference external" href="../users/image_tutorial-7.hires.png">hires.png</a>, <a class="reference external" href="../users/image_tutorial-7.pdf">pdf</a>)</p>
<div class="figure">
<img alt="../_images/image_tutorial-7.png" src="../_images/image_tutorial-7.png" />
</div>
</div>
<div class="section" id="array-interpolation-schemes">
<span id="interpolation"></span><h3>Array Interpolation schemes<a class="headerlink" href="#array-interpolation-schemes" title="Permalink to this headline">¶</a></h3>
<p>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.</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [8]: </span><span class="kn">import</span> <span class="nn">Image</span>
<span class="gp">In [9]: </span><span class="n">img</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">'stinkbug.png'</span><span class="p">)</span> <span class="c"># Open image as PIL image object</span>
<span class="gp">In [10]: </span><span class="n">rsize</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">/</span><span class="mi">10</span><span class="p">,</span><span class="n">img</span><span class="o">.</span><span class="n">size</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">/</span><span class="mi">10</span><span class="p">))</span> <span class="c"># Use PIL to resize</span>
<span class="gp">In [11]: </span><span class="n">rsizeArr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">rsize</span><span class="p">)</span> <span class="c"># Get array back</span>
<span class="gp">In [12]: </span><span class="n">imgplot</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">rsizeArr</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-8.py">Source code</a>)</p>
<p>Here we have the default interpolation, bilinear, since we did not
give <a class="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><tt class="xref py py-func docutils literal"><span class="pre">imshow()</span></tt></a> any interpolation argument.</p>
<p>Let’s try some others:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [10]: </span><span class="n">imgplot</span><span class="o">.</span><span class="n">set_interpolation</span><span class="p">(</span><span class="s">'nearest'</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-9.py">Source code</a>)</p>
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [10]: </span><span class="n">imgplot</span><span class="o">.</span><span class="n">set_interpolation</span><span class="p">(</span><span class="s">'bicubic'</span><span class="p">)</span>
</pre></div>
</div>
<p>(<a class="reference external" href="../users/image_tutorial-10.py">Source code</a>)</p>
<p>Bicubic interpolation is often used when blowing up photos - people
tend to prefer blurry over pixelated.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="artists.html" title="Artist tutorial"
>next</a> |</li>
<li class="right" >
<a href="annotations_intro.html" title="Annotating text"
>previous</a> |</li>
<li><a href="../index.html">home</a>| </li>
<li><a href="../examples/index.html">examples</a>| </li>
<li><a href="../gallery.html">gallery</a>| </li>
<li><a href="../api/pyplot_summary.html">pyplot</a>| </li>
<li><a href="../contents.html">docs</a> »</li>
<li><a href="index.html" >User’s Guide</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team; 2012 - 2013 The matplotlib development team.
Last updated on Oct 10, 2013.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b2.
</div>
</body>
</html>