ImageFile
模块#
这个 ImageFile
模块为图像打开和保存功能提供支持功能。
此外,它还提供了 Parser
类,可用于逐段解码图像(例如,通过网络连接接收图像时)。此类实现与标准相同的使用者接口 sgmllib 和 xmllib 模块。
示例:分析图像#
from PIL import ImageFile
fp = open("hopper.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")
Classes#
- class PIL.ImageFile.PyDecoder[源代码]#
基类:
PyCodec
Python实现的一个格式解码器。重写此类并将解码逻辑添加到
decode()
方法。看见 Writing Your Own File Codec in Python
- decode(buffer)[源代码]#
重写以执行解码过程。
- 参数:
buffer -- 带有要解码的数据的bytes对象。
- 返回:
的元组
(bytes consumed, errcode)
。如果完成解码,则为消耗的字节返回-1。错误代码来自ImageFile.ERRORS
。
- class PIL.ImageFile.PyEncoder[源代码]#
基类:
PyCodec
一种格式编码器的Python实现。重写此类并将解码逻辑添加到
encode()
方法。看见 Writing Your Own File Codec in Python
- encode(bufsize)[源代码]#
重写以执行编码过程。
- 参数:
bufsize -- 缓冲区大小。
- 返回:
一个元组
(bytes encoded, errcode, bytes)
。如果编码完成,则返回1作为错误代码。错误代码来自ImageFile.ERRORS
。
- encode_to_file(fh, bufsize)[源代码]#
- 参数:
fh -- 文件句柄。
bufsize -- 缓冲区大小。
- 返回:
如果成功完成,则返回0。否则,返回错误码。错误代码来自
ImageFile.ERRORS
。
- encode_to_pyfd()[源代码]#
如果
pushes_fd
是True
,则将使用此方法,并且encode()
将只被调用一次。- 返回:
一个元组
(bytes consumed, errcode)
。错误代码来自ImageFile.ERRORS
。
常量#
- PIL.ImageFile.LOAD_TRUNCATED_IMAGES = False#
是否加载截断的图像文件。用户代码可能会更改这一点。
- PIL.ImageFile.ERRORS#
从返回的已知错误代码的字典
PyDecoder.decode()
,PyEncoder.encode()
PyEncoder.encode_to_pyfd()
和PyEncoder.encode_to_file()
。