ImageFile module

The ImageFile module provides support functions for the image open and save functions.

In addition, it provides a Parser class which can be used to decode an image piece by piece (e.g. while receiving it over a network connection). This class implements the same consumer interface as the standard sgmllib and xmllib modules.

Example: Parse an image

from PIL import ImageFile

fp = open("hopper.ppm", "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._Tile[source]

Bases: NamedTuple

_Tile(codec_name, extents, offset, args)

codec_name: str

Alias for field number 0

extents: tuple[int, int, int, int] | None

Alias for field number 1

offset: int

Alias for field number 2

args: tuple[Any, ...] | str | None

Alias for field number 3

class PIL.ImageFile.Parser[source]

Incremental image parser. This class implements the standard feed/close consumer interface.

close() → Image[source]

(Consumer) Close the stream.

Returns:

An image object.

Raises:

OSError – If the parser failed to parse the image file either because it cannot be identified or cannot be decoded.

feed(data: bytes) → None[source]

(Consumer) Feed data to the parser.

Parameters:

data – A string buffer.

Raises:

OSError – If the parser failed to parse the image file.

reset() → None[source]

(Consumer) Reset the parser. Note that you can only call this method immediately after you’ve created a parser; parser instances cannot be reused.

class PIL.ImageFile.PyCodec[source]
cleanup() → None[source]

Override to perform codec specific cleanup

Returns:

None

init(args: tuple[Any, ...]) → None[source]

Override to perform codec specific initialization

Parameters:

args – Tuple of arg items from the tile entry

Returns:

None

setfd(fd: IO[bytes]) → None[source]

Called from ImageFile to set the Python file-like object

Parameters:

fd – A Python file-like object

Returns:

None

setimage(im: Image.core.ImagingCore, extents: tuple[int, int, int, int] | None = None) → None[source]

Called from ImageFile to set the core output image for the codec

Parameters:
  • im – A core image object

  • extents – a 4 tuple of (x0, y0, x1, y1) defining the rectangle for this tile

Returns:

None

class PIL.ImageFile.