2016-06-30 00:06:25 +03:00
|
|
|
|
.. _image-plugins:
|
2015-01-07 02:12:37 +03:00
|
|
|
|
|
2016-06-30 00:06:25 +03:00
|
|
|
|
Writing Your Own Image Plugin
|
2013-10-07 05:56:28 +04:00
|
|
|
|
=============================
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-07-03 09:38:30 +03:00
|
|
|
|
Pillow uses a plugin model which allows you to add your own
|
2022-02-25 08:07:01 +03:00
|
|
|
|
decoders and encoders to the library, without any changes to the library
|
|
|
|
|
itself. Such plugins usually have names like :file:`XxxImagePlugin.py`,
|
|
|
|
|
where ``Xxx`` is a unique format name (usually an abbreviation).
|
2013-11-13 11:08:47 +04:00
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
.. warning:: Pillow >= 2.1.0 no longer automatically imports any file
|
|
|
|
|
in the Python path with a name ending in
|
|
|
|
|
:file:`ImagePlugin.py`. You will need to import your
|
|
|
|
|
image plugin manually.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
2020-07-03 09:38:30 +03:00
|
|
|
|
Pillow decodes files in two stages:
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
1. It loops over the available image plugins in the loaded order, and
|
2020-05-17 09:05:54 +03:00
|
|
|
|
calls the plugin's ``_accept`` function with the first 16 bytes of
|
|
|
|
|
the file. If the ``_accept`` function returns true, the plugin's
|
2016-06-30 00:06:25 +03:00
|
|
|
|
``_open`` method is called to set up the image metadata and image
|
|
|
|
|
tiles. The ``_open`` method is not for decoding the actual image
|
|
|
|
|
data.
|
|
|
|
|
2. When the image data is requested, the ``ImageFile.load`` method is
|
|
|
|
|
called, which sets up a decoder for each tile and feeds the data to
|
|
|
|
|
it.
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-07-03 09:38:30 +03:00
|
|
|
|
An image plugin should contain a format handler derived from the
|
2016-07-05 15:04:11 +03:00
|
|
|
|
:py:class:`PIL.ImageFile.ImageFile` base class. This class should
|
2020-07-14 15:20:44 +03:00
|
|
|
|
provide an ``_open`` method, which reads the file header and
|
2016-07-05 15:04:11 +03:00
|
|
|
|
sets up at least the :py:attr:`~PIL.Image.Image.mode` and
|
|
|
|
|
:py:attr:`~PIL.Image.Image.size` attributes. To be able to load the
|
2020-07-14 15:20:44 +03:00
|
|
|
|
file, the method must also create a list of ``tile`` descriptors,
|
|
|
|
|
which contain a decoder name, extents of the tile, and
|
2017-02-23 13:20:25 +03:00
|
|
|
|
any decoder-specific data. The format handler class must be explicitly
|
2016-07-05 15:04:11 +03:00
|
|
|
|
registered, via a call to the :py:mod:`~PIL.Image` module.
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
.. note:: For performance reasons, it is important that the
|
2020-07-14 15:20:44 +03:00
|
|
|
|
``_open`` method quickly rejects files that do not have the
|
2016-07-08 12:52:30 +03:00
|
|
|
|
appropriate contents.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
2013-10-07 07:06:18 +04:00
|
|
|
|
Example
|
|
|
|
|
-------
|
|
|
|
|
|
2020-07-03 09:38:30 +03:00
|
|
|
|
The following plugin supports a simple format, which has a 128-byte header
|
2013-10-07 07:06:18 +04:00
|
|
|
|
consisting of the words “SPAM” followed by the width, height, and pixel size in
|
|
|
|
|
bits. The header fields are separated by spaces. The image data follows
|
|
|
|
|
directly after the header, and can be either bi-level, greyscale, or 24-bit
|
|
|
|
|
true color.
|
|
|
|
|
|
|
|
|
|
**SpamImagePlugin.py**::
|
|
|
|
|
|
|
|
|
|
from PIL import Image, ImageFile
|
|
|
|
|
|
2020-05-17 02:24:06 +03:00
|
|
|
|
|
|
|
|
|
def _accept(prefix):
|
|
|
|
|
return prefix[:4] == b"SPAM"
|
|
|
|
|
|
|
|
|
|
|
2013-10-07 07:06:18 +04:00
|
|
|
|
class SpamImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
|
|
format = "SPAM"
|
|
|
|
|
format_description = "Spam raster image"
|
|
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
2020-05-17 02:24:06 +03:00
|
|
|
|
header = self.fp.read(128).split()
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
# size in pixels (width, height)
|
2018-09-30 07:07:55 +03:00
|
|
|
|
self._size = int(header[1]), int(header[2])
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
# mode setting
|
|
|
|
|
bits = int(header[3])
|
|
|
|
|
if bits == 1:
|
|
|
|
|
self.mode = "1"
|
|
|
|
|
elif bits == 8:
|
|
|
|
|
self.mode = "L"
|
|
|
|
|
elif bits == 24:
|
|
|
|
|
self.mode = "RGB"
|
|
|
|
|
else:
|
2020-02-09 07:34:14 +03:00
|
|
|
|
raise SyntaxError("unknown number of bits")
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
# data descriptor
|
2020-07-03 09:38:30 +03:00
|
|
|
|
self.tile = [("raw", (0, 0) + self.size, 128, (self.mode, 0, 1))]
|
|
|
|
|
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-05-17 02:24:06 +03:00
|
|
|
|
Image.register_open(SpamImageFile.format, SpamImageFile, _accept)
|
2015-01-07 02:12:37 +03:00
|
|
|
|
|
2021-12-03 23:50:08 +03:00
|
|
|
|
Image.register_extensions(
|
|
|
|
|
SpamImageFile.format,
|
|
|
|
|
[
|
|
|
|
|
".spam",
|
|
|
|
|
".spa", # DOS version
|
|
|
|
|
],
|
|
|
|
|
)
|
2020-07-03 09:38:30 +03:00
|
|
|
|
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2016-06-30 00:06:25 +03:00
|
|
|
|
The format handler must always set the
|
|
|
|
|
:py:attr:`~PIL.Image.Image.size` and :py:attr:`~PIL.Image.Image.mode`
|
|
|
|
|
attributes. If these are not set, the file cannot be opened. To
|
2016-07-05 15:04:11 +03:00
|
|
|
|
simplify the plugin, the calling code considers exceptions like
|
2016-06-30 00:06:25 +03:00
|
|
|
|
:py:exc:`SyntaxError`, :py:exc:`KeyError`, :py:exc:`IndexError`,
|
|
|
|
|
:py:exc:`EOFError` and :py:exc:`struct.error` as a failure to identify
|
|
|
|
|
the file.
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
Note that the image plugin must be explicitly registered using
|
2013-10-07 07:06:18 +04:00
|
|
|
|
:py:func:`PIL.Image.register_open`. Although not required, it is also a good
|
|
|
|
|
idea to register any extensions used by this format.
|
|
|
|
|
|
2020-08-10 15:46:36 +03:00
|
|
|
|
Once the plugin has been imported, it can be used:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2020-08-10 14:57:18 +03:00
|
|
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import SpamImagePlugin
|
2021-12-03 23:50:08 +03:00
|
|
|
|
|
2020-08-10 14:57:18 +03:00
|
|
|
|
with Image.open("hopper.spam") as im:
|
|
|
|
|
pass
|
|
|
|
|
|
2020-07-14 15:20:44 +03:00
|
|
|
|
The ``tile`` attribute
|
|
|
|
|
----------------------
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-07-14 15:20:44 +03:00
|
|
|
|
To be able to read the file as well as just identifying it, the ``tile``
|
2013-10-07 07:06:18 +04:00
|
|
|
|
attribute must also be set. This attribute consists of a list of tile
|
|
|
|
|
descriptors, where each descriptor specifies how data should be loaded to a
|
|
|
|
|
given region in the image. In most cases, only a single descriptor is used,
|
|
|
|
|
covering the full image.
|
|
|
|
|
|
|
|
|
|
The tile descriptor is a 4-tuple with the following contents::
|
|
|
|
|
|
|
|
|
|
(decoder, region, offset, parameters)
|
|
|
|
|
|
|
|
|
|
The fields are used as follows:
|
|
|
|
|
|
|
|
|
|
**decoder**
|
|
|
|
|
Specifies which decoder to use. The ``raw`` decoder used here supports
|
|
|
|
|
uncompressed data, in a variety of pixel formats. For more information on
|
|
|
|
|
this decoder, see the description below.
|
|
|
|
|
|
|
|
|
|
**region**
|
|
|
|
|
A 4-tuple specifying where to store data in the image.
|
|
|
|
|
|
|
|
|
|
**offset**
|
|
|
|
|
Byte offset from the beginning of the file to image data.
|
|
|
|
|
|
|
|
|
|
**parameters**
|
|
|
|
|
Parameters to the decoder. The contents of this field depends on the
|
|
|
|
|
decoder specified by the first field in the tile descriptor tuple. If the
|
2020-07-10 00:50:57 +03:00
|
|
|
|
decoder doesn’t need any parameters, use :data:`None` for this field.
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-07-14 15:20:44 +03:00
|
|
|
|
Note that the ``tile`` attribute contains a list of tile descriptors,
|
2013-10-07 07:06:18 +04:00
|
|
|
|
not just a single descriptor.
|
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
Decoders
|
|
|
|
|
========
|
|
|
|
|
|
2015-01-07 01:39:57 +03:00
|
|
|
|
The raw decoder
|
|
|
|
|
---------------
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
The ``raw`` decoder is used to read uncompressed data from an image file. It
|
|
|
|
|
can be used with most uncompressed file formats, such as PPM, BMP, uncompressed
|
|
|
|
|
TIFF, and many others. To use the raw decoder with the
|
2020-11-28 04:10:20 +03:00
|
|
|
|
:py:func:`PIL.Image.frombytes` function, use the following syntax:
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-08-10 16:04:09 +03:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2016-01-04 12:46:06 +03:00
|
|
|
|
image = Image.frombytes(
|
2015-01-07 02:12:37 +03:00
|
|
|
|
mode, size, data, "raw",
|
2021-12-03 23:41:18 +03:00
|
|
|
|
raw_mode, stride, orientation
|
2013-10-07 07:06:18 +04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
When used in a tile descriptor, the parameter field should look like::
|
|
|
|
|
|
2021-12-03 23:41:18 +03:00
|
|
|
|
(raw_mode, stride, orientation)
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
The fields are used as follows:
|
|
|
|
|
|
2021-12-03 23:41:18 +03:00
|
|
|
|
**raw_mode**
|
2013-10-07 07:06:18 +04:00
|
|
|
|
The pixel layout used in the file, and is used to properly convert data to
|
|
|
|
|
PIL’s internal layout. For a summary of the available formats, see the
|
|
|
|
|
table below.
|
|
|
|
|
|
|
|
|
|
**stride**
|
|
|
|
|
The distance in bytes between two consecutive lines in the image. If 0, the
|
|
|
|
|
image is assumed to be packed (no padding between lines). If omitted, the
|
|
|
|
|
stride defaults to 0.
|
|
|
|
|
|
|
|
|
|
**orientation**
|
|
|
|
|
Whether the first line in the image is the top line on the screen (1), or
|
|
|
|
|
the bottom line (-1). If omitted, the orientation defaults to 1.
|
|
|
|
|
|
|
|
|
|
The **raw mode** field is used to determine how the data should be unpacked to
|
|
|
|
|
match PIL’s internal pixel layout. PIL supports a large set of raw modes; for a
|
2020-07-09 20:48:04 +03:00
|
|
|
|
complete list, see the table in the :file:`Unpack.c` module. The following
|
2013-10-07 07:06:18 +04:00
|
|
|
|
table describes some commonly used **raw modes**:
|
|
|
|
|
|
2021-07-02 11:59:12 +03:00
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| mode | description |
|
|
|
|
|
+===========+===================================================================+
|
|
|
|
|
| ``1`` | | 1-bit bilevel, stored with the leftmost pixel in the most |
|
|
|
|
|
| | | significant bit. 0 means black, 1 means white. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``1;I`` | | 1-bit inverted bilevel, stored with the leftmost pixel in the |
|
|
|
|
|
| | | most significant bit. 0 means white, 1 means black. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``1;R`` | | 1-bit reversed bilevel, stored with the leftmost pixel in the |
|
|
|
|
|
| | | least significant bit. 0 means black, 1 means white. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``L`` | 8-bit greyscale. 0 means black, 255 means white. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``L;I`` | 8-bit inverted greyscale. 0 means white, 255 means black. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``P`` | 8-bit palette-mapped image. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``RGB`` | 24-bit true colour, stored as (red, green, blue). |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``BGR`` | 24-bit true colour, stored as (blue, green, red). |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``RGBX`` | | 24-bit true colour, stored as (red, green, blue, pad). The pad |
|
|
|
|
|
| | | pixels may vary. |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
|
|
|
|
| ``RGB;L`` | | 24-bit true colour, line interleaved (first all red pixels, then|
|
|
|
|
|
| | | all green pixels, finally all blue pixels). |
|
|
|
|
|
+-----------+-------------------------------------------------------------------+
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
Note that for the most common cases, the raw mode is simply the same as the mode.
|
|
|
|
|
|
|
|
|
|
The Python Imaging Library supports many other decoders, including JPEG, PNG,
|
|
|
|
|
and PackBits. For details, see the :file:`decode.c` source file, and the
|
2020-07-03 09:38:30 +03:00
|
|
|
|
standard plugin implementations provided with the library.
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
|
|
|
|
Decoding floating point data
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
PIL provides some special mechanisms to allow you to load a wide variety of
|
|
|
|
|
formats into a mode ``F`` (floating point) image memory.
|
|
|
|
|
|
|
|
|
|
You can use the ``raw`` decoder to read images where data is packed in any
|
|
|
|
|
standard machine data type, using one of the following raw modes:
|
|
|
|
|
|
|
|
|
|
============ =======================================
|
2020-05-01 21:23:39 +03:00
|
|
|
|
mode description
|
2013-10-07 07:06:18 +04:00
|
|
|
|
============ =======================================
|
|
|
|
|
``F`` 32-bit native floating point.
|
|
|
|
|
``F;8`` 8-bit unsigned integer.
|
|
|
|
|
``F;8S`` 8-bit signed integer.
|
|
|
|
|
``F;16`` 16-bit little endian unsigned integer.
|
|
|
|
|
``F;16S`` 16-bit little endian signed integer.
|
|
|
|
|
``F;16B`` 16-bit big endian unsigned integer.
|
|
|
|
|
``F;16BS`` 16-bit big endian signed integer.
|
|
|
|
|
``F;16N`` 16-bit native unsigned integer.
|
|
|
|
|
``F;16NS`` 16-bit native signed integer.
|
|
|
|
|
``F;32`` 32-bit little endian unsigned integer.
|
|
|
|
|
``F;32S`` 32-bit little endian signed integer.
|
|
|
|
|
``F;32B`` 32-bit big endian unsigned integer.
|
|
|
|
|
``F;32BS`` 32-bit big endian signed integer.
|
|
|
|
|
``F;32N`` 32-bit native unsigned integer.
|
|
|
|
|
``F;32NS`` 32-bit native signed integer.
|
|
|
|
|
``F;32F`` 32-bit little endian floating point.
|
|
|
|
|
``F;32BF`` 32-bit big endian floating point.
|
|
|
|
|
``F;32NF`` 32-bit native floating point.
|
|
|
|
|
``F;64F`` 64-bit little endian floating point.
|
|
|
|
|
``F;64BF`` 64-bit big endian floating point.
|
|
|
|
|
``F;64NF`` 64-bit native floating point.
|
|
|
|
|
============ =======================================
|
|
|
|
|
|
|
|
|
|
The bit decoder
|
|
|
|
|
---------------
|
|
|
|
|
|
|
|
|
|
If the raw decoder cannot handle your format, PIL also provides a special “bit”
|
|
|
|
|
decoder that can be used to read various packed formats into a floating point
|
|
|
|
|
image memory.
|
|
|
|
|
|
2019-05-06 01:40:32 +03:00
|
|
|
|
To use the bit decoder with the :py:func:`PIL.Image.frombytes` function, use
|
2021-02-23 15:08:44 +03:00
|
|
|
|
the following syntax:
|
2013-10-07 07:06:18 +04:00
|
|
|
|
|
2020-08-10 16:04:09 +03:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2019-05-06 01:40:32 +03:00
|
|
|
|
image = Image.frombytes(
|
2013-10-07 07:06:18 +04:00
|
|
|
|
mode, size, data, "bit",
|
|
|
|
|
bits, pad, fill, sign, orientation
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
When used in a tile descriptor, the parameter field should look like::
|
|
|
|
|
|
|
|
|
|
(bits, pad, fill, sign, orientation)
|
|
|
|
|
|
|
|
|
|
The fields are used as follows:
|
|
|
|
|
|
|
|
|
|
**bits**
|
|
|
|
|
Number of bits per pixel (2-32). No default.
|
|
|
|
|
|
|
|
|
|
**pad**
|
|
|
|
|
Padding between lines, in bits. This is either 0 if there is no padding, or
|
|
|
|
|
8 if lines are padded to full bytes. If omitted, the pad value defaults to
|
|
|
|
|
8.
|
|
|
|
|
|
|
|
|
|
**fill**
|
|
|
|
|
Controls how data are added to, and stored from, the decoder bit buffer.
|
|
|
|
|
|
|
|
|
|
**fill=0**
|
|
|
|
|
Add bytes to the LSB end of the decoder buffer; store pixels from the MSB
|
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
**fill=1**
|
|
|
|
|
Add bytes to the MSB end of the decoder buffer; store pixels from the MSB
|
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
**fill=2**
|
|
|
|
|
Add bytes to the LSB end of the decoder buffer; store pixels from the LSB
|
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
**fill=3**
|
|
|
|
|
Add bytes to the MSB end of the decoder buffer; store pixels from the LSB
|
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
If omitted, the fill order defaults to 0.
|
|
|
|
|
|
|
|
|
|
**sign**
|
|
|
|
|
If non-zero, bit fields are sign extended. If zero or omitted, bit fields
|
|
|
|
|
are unsigned.
|
|
|
|
|
|
|
|
|
|
**orientation**
|
|
|
|
|
Whether the first line in the image is the top line on the screen (1), or
|
|
|
|
|
the bottom line (-1). If omitted, the orientation defaults to 1.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
.. _file-decoders:
|
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
Writing Your Own File Decoder in C
|
|
|
|
|
==================================
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
There are 3 stages in a file decoder's lifetime:
|
|
|
|
|
|
2016-07-05 15:04:11 +03:00
|
|
|
|
1. Setup: Pillow looks for a function in the decoder registry, falling
|
|
|
|
|
back to a function named ``[decodername]_decoder`` on the internal
|
|
|
|
|
core image object. That function is called with the ``args`` tuple
|
2016-06-30 00:06:25 +03:00
|
|
|
|
from the ``tile`` setup in the ``_open`` method.
|
|
|
|
|
|
2016-07-01 14:42:36 +03:00
|
|
|
|
2. Decoding: The decoder's decode function is repeatedly called with
|
2016-06-30 00:06:25 +03:00
|
|
|
|
chunks of image data.
|
|
|
|
|
|
|
|
|
|
3. Cleanup: If the decoder has registered a cleanup function, it will
|
|
|
|
|
be called at the end of the decoding process, even if there was an
|
|
|
|
|
exception raised.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Setup
|
2016-07-01 14:27:01 +03:00
|
|
|
|
-----
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
The current conventions are that the decoder setup function is named
|
|
|
|
|
``PyImaging_[Decodername]DecoderNew`` and defined in ``decode.c``. The
|
|
|
|
|
python binding for it is named ``[decodername]_decoder`` and is setup
|
|
|
|
|
from within the ``_imaging.c`` file in the codecs section of the
|
|
|
|
|
function array.
|
|
|
|
|
|
|
|
|
|
The setup function needs to call ``PyImaging_DecoderNew`` and at the
|
|
|
|
|
very least, set the ``decode`` function pointer. The fields of
|
|
|
|
|
interest in this object are:
|
|
|
|
|
|
2016-07-01 14:27:01 +03:00
|
|
|
|
**decode**
|
2016-06-30 00:06:25 +03:00
|
|
|
|
Function pointer to the decode function, which has access to
|
|
|
|
|
``im``, ``state``, and the buffer of data to be added to the image.
|
|
|
|
|
|
2016-07-01 14:27:01 +03:00
|
|
|
|
**cleanup**
|
2016-06-30 00:06:25 +03:00
|
|
|
|
Function pointer to the cleanup function, has access to ``state``.
|
|
|
|
|
|
2016-07-01 14:27:01 +03:00
|
|
|
|
**im**
|
|
|
|
|
The target image, will be set by Pillow.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
**state**
|
2020-07-10 23:12:20 +03:00
|
|
|
|
An ImagingCodecStateInstance, will be set by Pillow. The ``context``
|
2016-06-30 00:06:25 +03:00
|
|
|
|
member is an opaque struct that can be used by the decoder to store
|
2016-07-01 14:42:36 +03:00
|
|
|
|
any format specific state or options.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
**pulls_fd**
|
|
|
|
|
**EXPERIMENTAL** -- **WARNING**, interface may change. If set to 1,
|
|
|
|
|
``state->fd`` will be a pointer to the Python file like object. The
|
|
|
|
|
decoder may use the functions in ``codec_fd.c`` to read directly
|
|
|
|
|
from the file like object rather than have the data pushed through a
|
|
|
|
|
buffer. Note that this implementation may be refactored until this
|
|
|
|
|
warning is removed.
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 3.3.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Decoding
|
|
|
|
|
--------
|
|
|
|
|
|
|
|
|
|
The decode function is called with the target (core) image, the
|
|
|
|
|
decoder state structure, and a buffer of data to be decoded.
|
|
|
|
|
|
|
|
|
|
**Experimental** -- If ``pulls_fd`` is set, then the decode function
|
|
|
|
|
is called once, with an empty buffer. It is the decoder's
|
2016-07-01 14:42:36 +03:00
|
|
|
|
responsibility to decode the entire tile in that one call. The rest of
|
|
|
|
|
this section only applies if ``pulls_fd`` is not set.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
It is the decoder's responsibility to pull as much data as possible
|
|
|
|
|
out of the buffer and return the number of bytes consumed. The next
|
|
|
|
|
call to the decoder will include the previous unconsumed tail. The
|
|
|
|
|
decoder function will be called multiple times as the data is read
|
|
|
|
|
from the file like object.
|
|
|
|
|
|
2016-07-01 14:27:01 +03:00
|
|
|
|
If an error occurs, set ``state->errcode`` and return -1.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
2016-07-01 14:27:01 +03:00
|
|
|
|
Return -1 on success, without setting the errcode.
|
2016-06-30 00:06:25 +03:00
|
|
|
|
|
|
|
|
|
Cleanup
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
The cleanup function is called after the decoder returns a negative
|
|
|
|
|
value, or if there is a read error from the file. This function should
|
|
|
|
|
free any allocated memory and release any resources from external
|
|
|
|
|
libraries.
|
2016-07-05 15:04:11 +03:00
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
.. _file-codecs-py:
|
2016-07-05 15:04:11 +03:00
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
Writing Your Own File Codec in Python
|
|
|
|
|
=====================================
|
2016-07-05 15:04:11 +03:00
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
Python file decoders and encoders should derive from
|
|
|
|
|
:py:class:`PIL.ImageFile.PyDecoder` and :py:class:`PIL.ImageFile.PyEncoder`
|
|
|
|
|
respectively, and should at least override the decode or encode method.
|
|
|
|
|
They should be registered using :py:meth:`PIL.Image.register_decoder` and
|
|
|
|
|
:py:meth:`PIL.Image.register_encoder`. As in the C implementation of
|
|
|
|
|
the file codecs, there are three stages in the lifetime of a
|
|
|
|
|
Python-based file codec:
|
2016-07-05 15:04:11 +03:00
|
|
|
|
|
|
|
|
|
1. Setup: Pillow looks for the decoder in the registry, then
|
|
|
|
|
instantiates the class.
|
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
2. Transforming: The instance's ``decode`` method is repeatedly called with
|
|
|
|
|
a buffer of data to be interpreted, or the ``encode`` method is repeatedly
|
|
|
|
|
called with the size of data to be output.
|
2016-07-05 15:04:11 +03:00
|
|
|
|
|
2022-02-25 08:07:01 +03:00
|
|
|
|
3. Cleanup: The instance's ``cleanup`` method is called.
|