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")
類別¶
- class PIL.ImageFile._Tile[來源]¶
基礎:
NamedTuple
_Tile(codec_name, extents, offset, args)
- class PIL.ImageFile.PyDecoder[原始碼]¶
基底類別:
PyCodec
格式解碼器的 Python 實作。覆寫此類別,並在
decode()
方法中新增解碼邏輯。- decode(buffer: bytes | SupportsArrayInterface) tuple[int, int] [原始碼]¶
覆寫以執行解碼程序。
- 參數:
buffer – 包含要解碼資料的 bytes 物件。
- 傳回:
(消耗的位元組數, 錯誤碼)
的元組。如果解碼完成,則傳回 -1 作為消耗的位元組數。錯誤碼來自ImageFile.ERRORS
。
- class PIL.ImageFile.PyEncoder[原始碼]¶
基底類別:
PyCodec
格式編碼器的 Python 實作。覆寫此類別,並在
encode()
方法中新增解碼邏輯。- encode(bufsize: int) tuple[int, int, bytes] [原始碼]¶
覆寫以執行編碼程序。
- 參數:
bufsize – 緩衝區大小。
- 傳回:
(編碼的位元組數, 錯誤碼, 位元組)
的元組。如果編碼完成,則傳回 1 作為錯誤碼。錯誤碼來自ImageFile.ERRORS
。
常數¶
- PIL.ImageFile.LOAD_TRUNCATED_IMAGES = False¶
是否載入截斷的影像檔案。使用者程式碼可以更改此設定。
- PIL.ImageFile.ERRORS¶
從
PyDecoder.decode()
、PyEncoder.encode()
、PyEncoder.encode_to_pyfd()
和PyEncoder.encode_to_file()
返回的已知錯誤代碼字典。