[pil-handbook翻译]使用指南-4

图像序列(动画)

    PIL包含对图像序列(也叫动画格式)的基本支持。所支持的序列图格式包括FLI/FLC, GIF和一部分还在试验中的格式。TIFF同样可以包含超过一帧的图像。
    当你打开一个图像序列文件,PIL自动载入这个序列的第一帧。
    你可以使用seek和tell方法在不同的帧之间移动:

Example: Reading sequences
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
    如这个例子所示,当序列结束时你将得到一个EOFError异常。
    注意,在当前库版本中的大部分方法都只允许你称动到下一帧(如上面的例子所示)。如果要回溯图像,你可以重新打开.

    下面这个iterator类使你可以用for语名来循环图像序列:

Example: A sequence iterator class
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…

Postscript 打印

The Python Imaging Library includes functions toprint images, text and graphics on
Postscript printers. Here’s a simple example:
PIL包含在Postscript 打印机上打印图像,文本和图型所需的函数,以下是简单例子:
Example: Drawing Postscript
import Image
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()

 下一页 PIL目录 下一页

相关文章:

右舷

↑ Grab this Headline Animator

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

[...] 上一页 PIL目录 下一页 [...]

[...] 上一页 PIL目录 [...]

Leave a comment

(required)

(required)