python_图片/打开/显示/保存/
- 第一种方法
- 程序中操作图片,需要安装Pillow模块 pip install Pillow
from PIL import Image
img = Image.open('check_image.jpg')
img.show()
- 局限性是必须依赖电脑自带的图片浏览器打开图片文件,不方便
- 建议使用matplotlib的库来绘制图片进行显示
- 第二种方法
- 用一个matplotlib的库来绘制图片进行显示。是一个专业绘图的库,相当于matlab中的plot
- pip install matplotlib 直接安装
from PIL import Image
import matplotlib.pyplot as plt
img=Image.open('check_image.jpg')
plt.figure("check_image")
plt.imshow(img)
plt.show()
matplotlib相关的属性及方法
1, figure默认是带axis(轴线)的,如果没有需要,我们可以关掉
plt.axis('off')
2, print img.size
print img.mode
print img.format
3, 图片的保存
img.save('check_image.png')
这行代码不仅能保存图片,还是转换格式,如本例中,
将原来的jpg图片保存为了png图片
- 可以应用到12306图片验证码的识别中读取下载后验证码图片