jpegio – Support for JPEG image decoding
Available on these boards
- class jpegio.JpegDecoder(*, software_ok: bool = True)
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, TileGrid, ColorConverter, Colorspace import supervisor decoder = JpegDecoder() width, height = decoder.open("/sd/example.jpg") bitmap = Bitmap(width, height, 65535) decoder.decode(bitmap) tg = TileGrid(bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)) supervisor.runtime.display.root_group = tg while True: pass
Create a JpegDecoder
- Parameters:
software_ok (bool) – If True,
decodeuses the software decoder when the JPEG decoder peripheral cannot decode the image. If False,decoderaisesValueErrorinstead, including on chips without the peripheral.
- 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_SWAPPEDcolorspace.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 mustopena new JPEG. It is not possible to repeatedlydecodethe same jpeg data, even if it is to select different scales or crop regions from it.On chips with a JPEG decoder peripheral (ESP32-P4 and ESP32-S31),
decodeuses the peripheral for baseline color JPEGs when the data was opened from a buffer in DMA-capable memory,scaleis 0,bitmapholds 16-bit values, and no skip index is given. It is fastest whenbitmapis exactly the image size and the whole image is decoded at 0,0. Colors may differ slightly from the software decoder’s. Other images, and data opened from a file or stream, use the software decoder, or raiseValueErrorifsoftware_okis False.Some builds use a faster decoder for common JPEG types. Colors may differ slightly between decoders.
- Parameters:
bitmap (Bitmap) – Optional output buffer
scale (int) – Scale factor from 0 to 3, inclusive.
x (int) – Horizontal pixel location in bitmap where source_bitmap upper-left corner will be placed
y (int) – Vertical pixel location in bitmap where source_bitmap upper-left corner will be placed
x1 (int) – Minimum x-value for rectangular bounding box to be copied from the source bitmap
y1 (int) – Minimum y-value for rectangular bounding box to be copied from the source bitmap
x2 (int) – Maximum x-value (exclusive) for rectangular bounding box to be copied from the source bitmap
y2 (int) – Maximum y-value (exclusive) for rectangular bounding box to be copied from the source bitmap
skip_source_index (int) – bitmap palette index in the source that will not be copied, set to None to copy all pixels
skip_dest_index (int) – bitmap palette index in the destination bitmap that will not get overwritten by the pixels from the source