Python图像库PIL(Python Image Library)是python的第三方图像处理库。
PIL历史悠久,原来是只支持python2.x的版本的,后来出现了移植到python3的库pillow。pillow号称是friendly fork for PIL,其功能和PIL差不多,但是支持python3。
PIL那些最常用的特性与用法参考:http://www.effbot.org/imagingbook
库下载链接:http://www.pythonware.com/products/pil/
友情参考链接:Python图像库PIL的类Image及其方法介绍
Image使用前提
在使用Image之前,需要使用pip命令安装pillow
#安装pillow第三方库
pip install -U pillow
#安装之后可以使用下面的命令查看
pip list
Image常见函数用法
#读取图片open()
from PIL import Image #或import Image
with Image.open(filename) as image:
image.show()
#两种原型
#Image.open(file) ⇒ image
#Image.open(file, mode) ⇒ image
mode的选择如下图所示,选择时应填字符串类型。
#显示图片show()
image.show()
#转换图片,当前图像转换为其他模式convert()
image.convert(mode)
#获取图像大小
width,height = image.size
print(image.size)#返回值为宽度和高度的二元组(width, height)
#修改图片大小:图片不变形
image = image.resize((w