python处理图片(1):打开/显示/保存图片
基于python脚本语言开发的数字图片处理包,其实很多,比如PIL,Pillow, opencv, scikit-image等。
对比这些包,PIL和Pillow只提供最基础的数字图像处理,功能有限;opencv实际上是一个c++库,只是提供了python接口,更新速度非常慢。到现在python都发展到了3.5版本,而opencv只支持到python 2.7版本;scikit-image是基于scipy的一款图像处理包,它将图片作为numpy数组进行处理,正好与matlab一样,因此,我们最终选择scikit-image进行数字图像处理。
1.需要的环境
打开终端,查看是否安装skimage:
conda list | grep skimage
如果未安装,可以查询有哪些skimage的包可以安装:
anaconda search -t conda scikit-image
选择一个最新版本的包并安装:
anaconda show CEFCA/scikit-image
conda install --channel https://conda.anaconda.org/CEFCA scikit-image
2.打开和显示图片
from skimage import io import matplotlib.pyplot as plt img=io.imread("./2.jpg") plt.figure(num='fish',figsize=(8,8)) plt.subplot(2,2,1) plt.title("origin image") plt.imshow(img,plt.cm.gray) plt.subplot(2,2,2) plt.title('R channel') plt.imshow(img[:,:,0]) plt.subplot(2,2,3) plt.title('G channel') plt.imshow(img[:,:,1]) plt.subplot(2,2,4) plt.title('B channel') plt.imshow(img[:,:,2]) plt.axis('off') plt.show()
3.保存图片
plt.switch_backend('agg') plt.savefig('res.jpg')