# 图像教程

## 启动命令

``````In [1]: %matplotlib inline
``````

``````In [2]: import matplotlib.pyplot as plt
In [3]: import matplotlib.image as mpimg
In [4]: import numpy as np
``````

## 将图像数据导入到 NumPy 数组

``````In [5]: img=mpimg.imread('stinkbug.png')
Out[5]:
array([[[ 0.40784314,  0.40784314,  0.40784314],
[ 0.40784314,  0.40784314,  0.40784314],
[ 0.40784314,  0.40784314,  0.40784314],
...,
[ 0.42745098,  0.42745098,  0.42745098],
[ 0.42745098,  0.42745098,  0.42745098],
[ 0.42745098,  0.42745098,  0.42745098]],

...,
[[ 0.44313726,  0.44313726,  0.44313726],
[ 0.4509804 ,  0.4509804 ,  0.4509804 ],
[ 0.4509804 ,  0.4509804 ,  0.4509804 ],
...,
[ 0.44705883,  0.44705883,  0.44705883],
[ 0.44705883,  0.44705883,  0.44705883],
[ 0.44313726,  0.44313726,  0.44313726]]], dtype=float32)
``````

## 将 NumPy 数组绘制为图像

``````In [6]: imgplot = plt.imshow(img)
``````

### 对图像绘图应用伪彩色方案

``````In [7]: lum_img = img[:,:,0]
``````

``````In [8]: plt.imshow(lum_img)
``````

``````In [9]: plt.imshow(lum_img, cmap="hot")
``````

``````In [10]: imgplot = plt.imshow(lum_img)
In [11]: imgplot.set_cmap('spectral')
``````

### 颜色刻度参考

``````In [12]: imgplot = plt.imshow(lum_img)
In [13]: plt.colorbar()
``````

### 检查特定数据范围

``````In [14]: plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
``````

``````In [15]: imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
``````

### 数组插值方案

``````In [16]: from PIL import Image
In [17]: img = Image.open('../_static/stinkbug.png')
In [18]: img.thumbnail((64, 64), Image.ANTIALIAS) # resizes image in-place
In [19]: imgplot = plt.imshow(img)
``````

``````In [20]: imgplot = plt.imshow(img, interpolation="nearest")
``````

``````In [21]: imgplot = plt.imshow(img, interpolation="bicubic")
``````