官方文档:http://effbot.org/imagingbook/image.htm
1.打开图像并显示(注意这样show的话,会生成本地文件的): img=Image.open('1.jpg')img.show()2.转8位灰度图 greyimg=img.convert('L')greyimg.show()
3.获取图片某一像素点的 (R,G,B)值 from PIL import Image imagepath='1.jpg' img = Image.open(imagepath) if img.mode not in ('L', 'RGB'): img = img.convert('RGB') r, g, b = img.getpixel((10, 10)) 4.从图像中随机截取一部分
crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标系统的原点(0, 0)是左上角。
leftupx=random.randint(0,img.size[0]-64-1)leftupy= random.randint(0,img.size[1]-128-1)img1=img.crop((leftupx,leftupy,leftupx+64,leftupy+128)) 5.保存图像 im.save("2.jpg") 6.PIL中的Image和numpy中的数组array相互转换image转换成array img=np.array(image) array转换成image
注意img必须强转成uint8类型!如果是uint16的矩阵而不转为uint8的话,Image.fromarray这句会报错
from PIL import Imageim_arr = Image.fromarray(np.uint8(img)) 7.修改图像大小im.resize(size, filter) ⇒ image
返回图像的调整大小的副本。size参数以像素为单位给出请求的大小,作为2元组:(宽、高)。
The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.
滤波器参数可以是最接近的(使用最近邻)、双线性(在2x2环境中进行线性插值)、双三次样条(在4x4环境中进行三次样条插值)或反锯齿(高质量的下采样滤波器)。如果省略,或者图像模式为“1”或“P”,则将其设置为最接近的模式。
Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for large downsampling ratios (e.g. when creating thumbnails). You should use ANTIALIAS unless speed is much more important than quality.
请注意,当前版本的公益诉讼的双线性和双三次滤波器不适用于大的下采样率(例如,创建缩略图时)。除非速度比质量更重要,否则应该使用反锯齿。
(总之一般情况下第二个参数使用默认就好)
比如要将图片修改成64*64的im = im.resize(64,64) 8.复制图片 im = Image.open('1.jpg')im2 = im.copy() 9.添加水印,叠加函数,拼接图像基于keras中的PIL中的paste()函数可以给图片添加水印,也可以叠加函数,拼接图像,做到多张图片结果的可视化。
https://blog.csdn.net/s411081123/article/details/86255688
10.更多操作https://www.cnblogs.com/meitian/p/3699223.html
部分内容选自:传送门
https://www.sohu.com/a/328328613_571478