Python Image库简单处理图像

2022-10-14,,,

直接列举几个常用的函数,可在 中查看更多相关函数。

 1 from pil import image
 2 import numpy as np
 3 
 4 img = image.open('demo.jpg', 'r') # 打开图片,保存为image对象
 5 new_img = image.new('rgb', (512, 512), 'red') # 新建image对象,大小为512×512×3,红色
 6 new_img.putpixel((123, 123), (255, 255, 255)) # 将new_img的(123, 123)处像素颜色改为白色
 7 r, g, b = new_img.getpixel((123, 123)) # 获取new_img的(123, 123)处像素的值
 8 img = img.convert('l') # 将图片转为灰度图片
 9 img.show() # 显示图片
10 img.save('demo_l.jpg') # 保存图片
11 data = img.getdata() # 获取图像内容
12 img_mat = np.matrix(data) # 将image对象转为矩阵
13 new_img = image.fromarray(img_mat) # 将矩阵转为image对象,需要保证矩阵元素类型为uint8,否则会error

 

《Python Image库简单处理图像.doc》

下载本文的Word格式文档,以方便收藏与打印。