python 包之 Pillow 图像处理教程分享
作者:autofelix 时间:2022-02-22 01:22:48
一、安装
被认为是python官方图像处理库
PIL非常适合于图像归档以及图像的批处理任务。可以使用PIL创建缩略图,转换图像格式,打印图像等等
PIL支持众多的GUI框架接口,可以用于图像展示
PIL库同样支持图像的大小转换,图像旋转,以及任意的仿射变换
pip install Pillow
二、打开图片
from PIL import Image
im = Image.open("picture.jpg")
im.show()
三、转换格式并保存
from PIL import Image
im = Image.open("picture.jpg")
im.save("result.png")
四、创建缩略图
from PIL import Image
im = Image.open("picture.jpg")
im.thumbnail((128, 128))
im.save("result.jpg")
五、获取图片属性
获取图像的来源,如果图像不是从文件读取它的值就是None。
from PIL import Image
im = Image.open("picture.jpg")
print(im.format)
六、图片信息
from PIL import Image
im = Image.open("picture.jpg")
print(im.info)
七、调色板
如果图像的模式是“P”,则返回Image Palette类的实例;否则,将为None
from PIL import Image
im = Image.open("picture.jpg")
print(im.palette)
八、画板
使用给定的变量mode和size生成画板
from PIL import Image
im= Image.new("RGB", (128, 128), "#FF0000")
im.show()
九、图片模式
图像的模式,常见如下
L:8位像素,黑白
P:9位像素,使用调色板映射到任何其他模式
1:1位像素,黑白图像,存成8位像素
RGB:3*8位像素,真彩
RGBA:4*8位像素,真彩+透明通道
CMYK:4*8位像素,印刷四色模式或彩色印刷模式
YCbCr:3*8位像素,色彩视频格式
I:32位整型像素
F:33位浮点型像素
from PIL import Image
im = Image.open("picture.jpg")
print(im.mode)
十、模式转换
将当前图像转换为其他模式,并且返回新的图像
from PIL import Image
im = Image.open("picture.jpg")
new_im = im.convert('L')
print(new_im.mode)
new_im.show()
十一、矩阵模式转换
使用转换矩阵将一个“RGB”图像转换为“L”或者“RGB”图像
from PIL import Image
im = Image.open("picture.jpg")
print(im.mode)
matrix = (0.412453,0.357580, 0.180423, 0,
0.212671,0.715160, 0.072169, 0,
0.019334,0.119193, 0.950227, 0 )
new_im = im.convert("L", matrix)
print(new_im.mode)
new_im.show()
十二、图片尺寸
获取图像的尺寸,按照像素数计算,它的返回值为宽度和高度的二元组
from PIL import Image
im = Image.open("picture.jpg")
print(im.size)
?
十三、通道分离
返回当前图像各个通道组成的一个元组
分离一个 RGB 图像将产生三个新的图像
分别对应原始图像的每个通道红、绿、蓝三张图片
from PIL import Image
im = Image.open("picture.jpg")
r,g,b = im.split()
print(r.mode)
print(r.size)
print(im.size)
十四、复制、裁剪、粘贴、合并
from PIL import Image
im = Image.open("picture.jpg")
# 复制
im.copy()
# 裁剪
im.crop((100, 100, 400, 400))
# 将一张图粘贴到另一张图像上
im.paste(im.transpose(Image.ROTATE_180),im)
# 合并类使用一些单通道图像,创建一个新的图像
Image.merge("RGB", im.split())
十五、几何变换
Image类有resize()、rotate()和transpose()、transform()方法进行几何变换
from PIL import Image
im = Image.open("picture.jpg")
# 修改尺寸
im.resize((128, 128))
# 角度旋转
im.rotate(45)
# 返回当前图像的翻转或者旋转的拷贝
# 值为:FLIP_LEFT_RIGHT,FLIP_TOP_BOTTOM,ROTATE_90,ROTATE_180,ROTATE_270
im.transpose(Image.ROTATE_90)
# 用给定的尺寸生成一张新的图像,与原图有相同的模式
im.transform((200, 200), Image.EXTENT, (0, 0, 300, 300))
十六、高级图片处理
使用
ImageEnhance
对象就能快速地进行设置。 可以调整对比度、亮度、色平衡和锐利度
from PIL import Image
from PIL import ImageEnhance
im = Image.open("picture.jpg")
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")
十七、滤波器处理
from PIL import Image
from PIL import ImageFilter
im = Image.open("picture.jpg")
# 均值滤波
im1 = im.filter(ImageFilter.BLUR)
# 找轮廓
im2 = im.filter(ImageFilter.CONTOUR)
# 边缘检测
im3 = im.filter(ImageFilter.FIND_EDGES)
im.show()
im1.show()
im2.show()
im3.show()
十八、设置透明度合成两张图
使用给定的两张图像及透明度变量alpha,生成一张新的图像
变量alpha值在0和1之间
若变量alpha为0.0,返回第一张图像的拷贝。若变量alpha为1.0,将返回第二张图像的拷贝。
from PIL import Image
im1 = Image.open("picture1.jpg")
im2 = Image.open("picture2.jpg")
im = Image.blend(im1, im2, 0.30)
im.show()
十九、设置模式合成两张图
使用给定的两张图像及mask图像作为透明度,生成一张新的图像
图像的模式可以为“1”,“L”或者“RGBA”
所有图像必须有相同的尺寸
from PIL import Image
im1 = Image.open("picture1.jpg")
im2 = Image.open("picture2.jpg")
im = Image.composite(im1, im2, 'L')
im.show()
二十、草稿模式
速度要求比质量高的场合
允许在不读取文件内容的情况下尽可能(可能不会完全等于给定的参数)地将图片转成给定模式和大小,这在生成缩略图的时候非常有效
from PIL import Image
im = Image.open("picture.jpg")
print(im.size,im.mode)
new_im = im.draft("L", (200,200))
print(new_im.size,new_im.mode)
new_im.show()
二十一、获取通道名称
返回包括每个通道名称的元组
例如,对于RGB图像将返回(“R”,“G”,“B”)
from PIL import Image
im = Image.open("picture.jpg")
print(im.getbands())
二十二、获取包围盒
计算图像非零区域的包围盒
这个包围盒是一个4元组,定义了左、上、右和下像素坐标
如果图像是空的,这个方法将返回空
from PIL import Image
im = Image.open("picture.jpg")
print(im.getbbox())
二十三、获取像素值
from PIL import Image
im = Image.open("picture.jpg")
print(im.getdata())
二十四、获取图片极值
返回一个2元组
包括该图像中的最小和最大值
from PIL import Image
im = Image.open("picture.jpg")
print(im.getextrema())
二十五、指定位置像素值
from PIL import Image
im = Image.open("picture.jpg")
print(im.getpixel((10,0)))
二十六、获取图像直方图
from PIL import Image
im = Image.open("picture.jpg")
im_histogram = im.histogram()
print(im_histogram[0])
二十七、内存分配
为图像分配内存并从文件中加载它
返回一个用于读取和修改像素的像素访问对象
from PIL import Image
im = Image.open("picture.jpg")
pix = im.load()
print(pix[0,2])
二十八、查找指定的帧
给定的动态图中查找指定的帧
如果查找超越了序列的末尾,则产生一个EOFError异常
当文件被打开时,PIL库自动指定到第0帧上
from PIL import Image
im_gif = Image.open("loading.gif")
print(im_gif.mode)
# 默认打开时是第0帧
im_gif.show()
im_gif.seek(1)
im_gif.show()
im_gif.seek(3)
im_gif.show()
二十九、获取当前帧位置
from PIL import Image
im_gif = Image.open("loading.gif")
print(im_gif.tell())
im_gif.seek(3)
print(im_gif.tell())
来源:https://blog.51cto.com/autofelix/5191379
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入了解Python iter() 方法的用法
python 判断一个进程是否存在
PyCharm+Miniconda3安装配置教程详解
![](https://img.aspxhome.com/file/2023/7/79627_0s.png)
在Python中处理日期和时间的基本知识点整理汇总
![](https://img.aspxhome.com/file/2023/9/89079_0s.jpg)
php读取xml实例代码
Go语言编程中对文件读写的基本方法整理
各浏览器 CSS Hack 整理
一个超级简单的python web程序
Python numpy下几种fft函数的使用方式
![](https://img.aspxhome.com/file/2023/0/107180_0s.png)
Apache DophinScheduler定时调度Python脚本的实现
![](https://img.aspxhome.com/file/2023/0/93780_0s.png)
自然选择:自然界的颜色与界面设计[译]
![](https://img.aspxhome.com/file/UploadPic/20099/19/01-69s.jpg)
python 列表输出重复值以及对应的角标方法
windows 下python+numpy安装实用教程
Python3如何将源目录中的图片用MD5命名并可以设定目标目录
Python打包方法Pyinstaller的使用
![](https://img.aspxhome.com/file/2023/5/103645_0s.png)
如何用Python来搭建一个简单的推荐系统
Python语言实现获取主机名根据端口杀死进程
设计哲学与跨界
Python实现的凯撒密码算法示例
![](https://img.aspxhome.com/file/2023/9/121409_0s.png)
一篇文章带你详细了解python中一些好用的库
![](https://img.aspxhome.com/file/2023/4/99044_0s.jpg)