Skip to content

Added JPG support #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions adafruit_imageload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def load(
palette is the desired palette type. The constructor should take the number of colors and
support assignment to indices via [].
"""
# pylint: disable=too-many-branches
if not bitmap or not palette:
try:
# use displayio if available
Expand Down Expand Up @@ -89,4 +90,8 @@ def load(
from . import png

return png.load(file, bitmap=bitmap, palette=palette)
if header.startswith(b"\xff\xd8"):
from . import jpg

return jpg.load(file, bitmap=bitmap)
raise RuntimeError("Unsupported image format")
56 changes: 56 additions & 0 deletions adafruit_imageload/jpg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2024 Channing Ramos
#
# SPDX-License-Identifier: MIT

"""
`adafruit_imageload.jpg`
====================================================

Load a JPG into a bitmap by calling the jpegio class.

* Author(s): Channing Ramos

"""

# A separate try for jpegio. Not every board supports it and this import may fail.
# If that happens an ImportError with a proper message needs to be raised
try:
from jpegio import JpegDecoder
except ImportError:
print("jpegio not supported on this board.")

try:
from io import BufferedReader
from typing import Tuple, Optional
from .displayio_types import BitmapConstructor
except ImportError:
pass

from displayio import Bitmap, ColorConverter, Colorspace

__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"


def load(
file: BufferedReader,
*,
bitmap: BitmapConstructor,
) -> Tuple[Bitmap, Optional[ColorConverter]]:
"""
Loads a JPG image from the open ''file''.
The JPG must be a Baseline JPG, Progressive and Lossless JPG formats are not supported.

Returns tuple of bitmap object and ColorConverter object.

:param io.BufferedReader file: Open file handle or compatible (like 'io.BytesIO')
:param object bitmap: Type to store bitmap data.
Must have API similar to 'displayio.Bitmap'. Will be skipped if None.
Will be skipped if None.
"""
decoder = JpegDecoder()
width, height = decoder.open(file)
bitmap_obj = bitmap(width, height, 65535)
decoder.decode(bitmap_obj)

return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)
22 changes: 22 additions & 0 deletions examples/imageload_jpg_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2024 Channing Ramos
#
# SPDX-License-Identifier: MIT

"""
Basic JPG imageload example
"""

import board
import displayio
import adafruit_imageload

group = displayio.Group()
board.DISPLAY.root_group = group

image, color_converter = adafruit_imageload.load("images/jpg_test.jpg")

tile_grid = displayio.TileGrid(image, pixel_shader=color_converter)
group.append(tile_grid)

while True:
pass
Binary file added examples/images/jpg_test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/images/jpg_test.jpg.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2024 Channing Ramos
# SPDX-License-Identifier: MIT
Loading