Python Imaging Library: ImageFile Module(图像文件模块)
ImageFile模块为图像打开和保存功能提供了支持函数。
此外,它还提供了一个解析器类,可以被用来对图像片段进行解码(例如,当通过网络连接接收图像时)。这个类实现了与标准sgmllib和xmllib模块相同的使用者接口。
例程:解析图像
import ImageFile
fp = open("lena.pgm", "rb")
p = ImageFile.Parser()
while 1:
s = fp.read(1024)
if not s:
break
p.feed(s)
im = p.close()
im.save("copy.jpg")
fp = open("lena.pgm", "rb")
p = ImageFile.Parser()
while 1:
s = fp.read(1024)
if not s:
break
p.feed(s)
im = p.close()
im.save("copy.jpg")
函数
Parser
ImageFile.Parser() ⇒ Parser instance
创建一个解析器对象。解析器不能重用。
方法
feed
parser.feed(data)
向解析器提供一串数据。这个方法可能会引起一个IOError异常。
close
parser.close() ⇒ image or None
通知解析器完成解码。如果解析器成功地解码了一个图像,它将返回一个图像对象。否则,该方法会引发一个IOError异常。
Note: If the file cannot be identified, the parser will raise an IOError exception in the close method. If the file can be identified, but not decoded (for example, if the data is damaged, or if it uses an unsupported compression method), the parser will raise an IOError exception as soon as possible, either in feed or close.