Python | 您所在的位置:网站首页 › python中读取数据集 › Python |
PIL即Python Imageing Library,而Pillow是PIL的一个分支。Pillow提供了常见的图像读取和处理的操作,它比opencv更为轻巧,而且可以与ipython notebook无缝集成。 使用Image.open()读取图片存储为一个对象,并非是numpy矩阵。 下面我们看看相关的代码操作: from PIL import Image #导入PIL库 import numpy as np img = Image.open('../xx.jpg') # 读取图片 imgL = Image.open('../xx.jpg').convert('L') # 读取图片灰度图 imgL.show() # 展示灰度图 img1 = img.copy() # 复制图片 print(img.format) # 输出图片格式 print(img.size) # 输出图片(宽度w,高度h) print(img.mode) # 输出图片类型,L为灰度图,RGB为真彩色,RGBA为RGB+Alpha透明度 im.show() # 展示画布 imgData = np.array(img) # 将对象img转化为RGB像素值矩阵 print(imgData.shape) # 输出图片(宽度w,高度h,通道c) print(imgData.dtype) # 输出图片类型,uint8为[0-255] print(imgData) # 输出所有像素的RGB值 imgN = Image.fromarray(imgData) # 将RGB像素值矩阵转化为对象imgN imgN.save('xxx.jpg') # 储存为文件xxx.jpg r ,g ,b = img.split() # 分离通道 img = Image.merge("RGB", (b, g, r)) # 合并通道 # ROI(region of interest),只对ROI区域操作 roi = img.crop((0, 0, 300, 300)) # (左上x,左上y,右下x,右下y)坐标 roi.show() # 展示ROI区域 #捕捉异IOError,为读取图片失败 try: img = Image.open('xxx.jpg') except IOError: print('image failed to read') |
CopyRight 2018-2019 实验室设备网 版权所有 |