jpegio – Support for JPEG image decoding — Adafruit CircuitPython 1 documentation (original) (raw)

Adafruit CircuitPython

Available on these boards

class jpegio.JpegDecoder

A JPEG decoder

A JpegDecoder allocates a few thousand bytes of memory. To reduce memory fragmentation, create a single JpegDecoder object and use it anytime a JPEG image needs to be decoded.

Example:

from jpegio import JpegDecoder from displayio import Bitmap

decoder = JpegDecoder() width, height = decoder.open("/sd/example.jpg") bitmap = Bitmap(width, height, 65535) decoder.decode(bitmap)

.. do something with bitmap

Create a JpegDecoder

open(filename: str) → Tuple[int, int]

open(buffer: circuitpython_typing.ReadableBuffer) → Tuple[int, int]

open(bytesio: io.BytesIO) → Tuple[int, int]

Use the specified object as the JPEG data source.

The source may be a filename, a binary buffer in memory, or an opened binary stream.

The single parameter is positional-only (write open(f), notopen(filename=f) but due to technical limitations this is not shown in the function signature in the documentation.

Returns the image size as the tuple (width, height).

decode(bitmap: displayio.Bitmap, scale: int = 0, x: int = 0, y: int = 0, *, x1: int, y1: int, x2: int, y2: int, skip_source_index: int, skip_dest_index: int) → None

Decode JPEG data

The bitmap must be large enough to contain the decoded image. The pixel data is stored in the displayio.Colorspace.RGB565_SWAPPED colorspace.

The image is optionally downscaled by a factor of 2**scale. Scaling by a factor of 8 (scale=3) is particularly efficient in terms of decoding time.

The remaining parameters are as for bitmaptools.blit. Because JPEG is a lossy data format, chroma keying based on the “source index” is not reliable, because the same original RGB value might end up being decompressed as a similar but not equal color value. Using a higher JPEG encoding quality can help, but ultimately it will not be perfect.

After a call to decode, you must open a new JPEG. It is not possible to repeatedly decode the same jpeg data, even if it is to select different scales or crop regions from it.

Parameters: