目录
一.图像基础处理操作
本次实验使用的是Python语言的PIL(Python Image Library)库,PIL库支持图像储存、显示和处理,他能够处理几乎所有图片格式,可以完成对图像的缩放、剪裁、叠加以及向图像添加线条、图像和文字等操作。
1.1.图像读取:
from PIL import Image
img = Image.open('jmu.jpg')
Image类是PIL中的核心类,上述为对图片的读取操作;
1.2.灰度变化:
图像的颜色转换可以用convert()方法实现,需要读取图像的灰度图像,可以在.open后加入convert(‘L’);
img = Image.open('jmu.jpg').convert('L')
1.3.图片显示:
plt.imshow(img1)
原图像显示和图像灰度图显示结果:
由于 plt是直接显示三通道的图像,灰度图是单通道的,可以使用
plt.imshow(img2,plt.cm.gray)
来对灰度图显示;
1.4.相关代码:
from PIL import Image
import matplotlib.pyplot as plt
img1 = Image.open('jmu.jpg')
img2 = Image.open('jmu.jpg').convert('L')
plt.subplot(1,2,1)
plt.title('jmu initial')
plt.imshow(img1)
plt.subplot(1,2,2)
plt.title('jmu trans')
plt.imshow(img2,plt.cm.gray)
plt.show()