安装
刚接触Pillow的朋友先来看一下Pillow的安装方法,在这里我们以Mac OS环境为例:
(1)、使用 pip 安装 Python 库。pip 是 Python 的包管理工具,安装后就可以直接在命令行一站式地安装/管理各种库了(pip 文档)。
$ wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz $ tar xzf pip-0.7.2.tar.gz $ cd pip-0.7.2 $ python setup.py install</div>
(2)、使用 pip 下载获取 Pillow:
$ pip install pillow</div>
(3)、安装过程中命令行出现错误提示:”error: command ‘clang' failed with exit status 1”。上网查阅,发现需要通过 Xcode 更新 Command Line Tool。于是打开 Xcode->Preferences->Downloads-Components选项卡。咦?竟然没了 Command Line Tools。再查,发现 Xcode 5 以上现在需要用命令行安装:
$ xcode-select —install</div>
系统会弹出安装命令行工具的提示,点击安装即可。
此时再 pip install pillow,就安装成功了。
pip freeze 命令查看已经安装的 Python 包,Pillow 已经乖乖躺那儿了。
好了,下面开始进入教程~
Image类
Pillow中最重要的类就是Image,该类存在于同名的模块中。可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建一个图片。
使用Image模块中的open函数打开一张图片:
>>> from PIL import Image
>>> im = Image.open("lena.ppm")
</div>
如果打开成功,返回一个Image对象,可以通过对象属性检查文件内容
>>> from __future__ import print_function >>> print(im.format, im.size, im.mode)</div>
PPM (512, 512) RGB</div>
format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。
如果文件不能打开,则抛出IOError异常。
当有一个Image对象时,可以用Image类的各个方法进行处理和操作图像,例如显示图片:
>>> im.show()</div>
ps:标准版本的show()方法不是很有效率,因为它先将图像保存为一个临时文件,然后使用xv进行显示。如果没有安装xv,该函数甚至不能工作。但是该方法非常便于debug和test。(windows中应该调用默认图片查看器打开)
读写图片
Pillow库支持相当多的图片格式。直接使用Image模块中的open()函数读取图片,而不必先处理图片的格式,Pillow库自动根据文件决定格式。
Image模块中的save()函数可以保存图片,除非你指定文件格式,那么文件名中的扩展名用来指定文件格式。
图片转成jpg格式
from __future__ import print_function
import os, sys
from PIL import Image
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print("cannot convert", infile)
</div>
save函数的第二个参数可以用来指定图片格式,如果文件名中没有给出一个标准的图像格式,那么第二个参数是必须的。
创建缩略图
from __future__ import print_function
import os, sys
from PIL import Image
size = (128, 128)
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile, "JPEG")
except IOError:
print("cannot create thumbnail for", infile)
</div>
必须指出的是除非必须,Pillow不会解码或raster数据。当你打开一个文件,Pillow通过文件头确定文件格式,大小,mode等数据,余下数据直到需要时才处理。
这意味着打开文件非常快,与文件大小和压缩格式无关。下面的程序用来快速确定图片属性:
确定图片属性
from __future__ import print_function import sys from PIL import Image for infile in sys.argv[1:]: try: with Image.open(infile) as im: print(infile, im.format, "%dx%d" % im.size, im.mode) except IOError: pass</div>
裁剪、粘贴、与合并图片
Image类包含还多操作图片区域的方法。如crop()方法可以从图片中提取一个子矩形
从图片中复制子图像
box = im.copy() #直接复制图像 box = (100, 100, 400, 400) region = im.crop(box)</div>
区域由4-tuple决定,该tuple中信息为(left, upper, right, lower)。 Pillow左边系统的原点(0,0)为图片的左上角。坐标中的数字单位为像素点,所以上例中截取的图片大小为300*300像素^2。
处理子图,粘贴回原图
region = region.transpose(Image.ROTATE_180) im.paste(region, box)</div>
将子图paste回原图时,子图的region必须和给定box的region吻合。该region不能超过原图。而原图和region的mode不需要匹配,Pillow会自动处理。
另一个例子
Rolling an image def roll(image, delta): "Roll an image sideways" image = image.copy() #复制图像 xsize, ysize = image.size delta = delta % xsize if delta == 0: return image part1 = image.crop((0, 0, delta, ysize)) part2 = image.crop((delta, 0, xsize, ysize)) image.paste(part2, (0, 0, xsize-delta, ysize)) image.paste(part1, (xsize-delta, 0, xsize, ysize)) return image</div>
分离和合并通道
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
</div>
对于单通道图片,split()返回图像本身。为了处理单通道图片,必须先将图片转成RGB。
几何变换
Image类有resize()、rotate()和transpose()、transform()方法进行几何变换。
简单几何变换
out = im.resize((128, 128)) out = im.rotate(45) # 顺时针角度表示</div>
置换图像
out = im.transpose(Image.FLIP_LEFT_RIGHT) out = im.transpose(Image.FLIP_TOP_BOTTOM) out = im.transpose(Image.ROTATE_90) out = im.transpose(Image.ROTATE_180) out = im.transpose(Image.ROTATE_270)</div>
transpose()和象的rotate()没有性能差别。
更通用的图像变换方法可以使用transform()
模式转换
convert()方法
模式转换
im = Image.open('lena.ppm').convert('L')
</div>
图像增强
Filter
ImageFilter模块包含很多预定义的增强filters,通过filter()方法使用
应用filters
from PIL import ImageFilter out = im.filter(ImageFilter.DETAIL)</div>
像素点处理
point()方法通过一个函数或者查询表对图像中的像素点进行处理(例如对比度操作)。
像素点变换
# multiply each pixel by 1.2 out = im.point(lambda i: i * 1.2)</div>
上述方法可以利用简单的表达式进行图像处理,通过组合point()和paste()还能选择性地处理图片的某一区域。
处理单独通道
# split the image into individual bands source = im.split() R, G, B = 0, 1, 2 # select regions where red is less than 100 mask = source[R].point(lambda i: i < 100 and 255) # process the green band out = source[G].point(lambda i: i * 0.7) # paste the processed band back, but only where red was < 100 source[G].paste(out, None, mask) # build a new multiband image im = Image.merge(im.mode, source)

