Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111207
  • 博文数量: 26
  • 博客积分: 1726
  • 博客等级: 上尉
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-11 14:24
文章分类

全部博文(26)

文章存档

2013年(2)

2011年(2)

2010年(15)

2008年(7)

我的朋友

分类: Python/Ruby

2010-05-30 11:55:51

图像序列(动画)

    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()

阅读(494) | 评论(0) | 转发(0) |
0

上一篇:pil-handbook翻译-5

下一篇:pil-handbook翻译-3

给主人留下些什么吧!~~