jpegio
– Support for JPEG image decoding
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
- 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 mustopen
a new JPEG. It is not possible to repeatedlydecode
the same jpeg data, even if it is to select different scales or crop regions from it.- 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