置换图像
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)transpose()和象的rotate()没有性能差别。
更通用的图像变换方法可以使用
方法
模式转换
im = Image.open('lena.ppm').convert('L')模块包含很多预定义的增强filters,通过方法使用
应用filters
from PIL import ImageFilter out = im.filter(ImageFilter.DETAIL)方法通过一个函数或者查询表对图像中的像素点进行处理(例如对比度操作)。
像素点变换
# multiply each pixel by 1.2 out = im.point(lambda i: i * 1.2)上述方法可以利用简单的表达式进行图像处理,通过组合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)注意到创建mask的语句:
mask = source[R].point(lambda i: i < 100 and 255)该句可以用下句表示
imout = im.point(lambda i: expression and 255)如果expression为假则返回expression的值为0(因为and语句已经可以得出结果了),否则返回255。(参数用法:当为0时,保留当前值,255为使用paste进来的值,中间则用于transparency效果)
对其他高级图片增强,应该使用模块 。一旦有一个Image对象,应用ImageEnhance对象就能快速地进行设置。 可以使用以下方法调整对比度、亮度、色平衡和锐利度。
图像增强
from PIL import ImageEnhance enh = ImageEnhance.Contrast(im) enh.enhance(1.3).show("30% more contrast")动态图
pillow支持一些动态图片的格式如FLI/FLC,GIF和其他一些处于实验阶段的格式。TIFF文件同样可以包含数帧图像。
当读取动态图时,PIL自动读取动态图的第一帧,可以使用seek和tell方法读取不同帧。
from PIL import Image im = Image.open("animation.gif") im.seek(1) # skip to the second frame try: while 1: im.seek(im.tell()+1) # do something to im except EOFError: pass # end of sequence当读取到最后一帧时,pillow抛出EOFError异常。
当前版本只允许seek到下一帧。为了倒回之前,必须重新打开文件。
或者可以使用下述迭代器类
动态图迭代器类
class ImageSequence: def __init__(self, im): self.im = im def __getitem__(self, ix): try: if ix: self.im.seek(ix) return self.im except EOFError: raise IndexError # end of sequence for frame in ImageSequence(im): # ...do something to frame...pillow允许通过Postscript Printer在图片上添加images、text、graphics。
Drawing Postscript
from PIL import Image from PIL import PSDraw im = Image.open("lena.ppm") title = "lena" box = (1*72, 2*72, 7*72, 10*72) # in points ps = PSDraw.PSDraw() # default is sys.stdout ps.begin_document(title) # draw the image (75 dpi) ps.image(box, im, 75) ps.rectangle(box) # draw centered title ps.setfont("HelveticaNarrow-Bold", 36) w, h, b = ps.textsize(title) ps.text((4*72-w/2, 1*72-h), title) ps.end_document()ps:textsize不能用,有谁知道吗
之前说到Image模块的open()函数已经足够日常使用。该函数的参数也可以是一个文件对象。
从string中读取
import StringIO im = Image.open(StringIO.StringIO(buffer))从tar文件中读取
from PIL import TarIO fp = TarIO.TarIO("Imaging.tar", "Imaging/test/lena.ppm") im = Image.open(fp)draft()方法允许在不读取文件内容的情况下尽可能(可能不会完全等于给定的参数)地将图片转成给定模式和大小,这在生成缩略图的时候非常有效(速度要求比质量高的场合)。
draft模式
from __future__ import print_function im = Image.open(file) print("original =", im.mode, im.size) im.draft("L", (100, 100)) print("draft =", im.mode, im.size)posted @
以上就是关于pillow的全部内容,相信你一定会非常满意。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-15774-2.html
促使叙利亚女青年踊跃到中国来
送测
12海里指的是哪里