Skip to content

Commit 6b7deeb

Browse files
authored
Merge pull request #81 from ch4nsuk3/jpg-support
Added JPG support
2 parents 72a7a4a + 9b9cbf4 commit 6b7deeb

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

adafruit_imageload/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def load(
4747
palette is the desired palette type. The constructor should take the number of colors and
4848
support assignment to indices via [].
4949
"""
50+
# pylint: disable=too-many-branches
5051
if not bitmap or not palette:
5152
try:
5253
# use displayio if available
@@ -89,4 +90,8 @@ def load(
8990
from . import png
9091

9192
return png.load(file, bitmap=bitmap, palette=palette)
93+
if header.startswith(b"\xff\xd8"):
94+
from . import jpg
95+
96+
return jpg.load(file, bitmap=bitmap)
9297
raise RuntimeError("Unsupported image format")

adafruit_imageload/jpg.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# SPDX-FileCopyrightText: 2024 Channing Ramos
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_imageload.jpg`
7+
====================================================
8+
9+
Load a JPG into a bitmap by calling the jpegio class.
10+
11+
* Author(s): Channing Ramos
12+
13+
"""
14+
15+
# A separate try for jpegio. Not every board supports it and this import may fail.
16+
# If that happens an ImportError with a proper message needs to be raised
17+
try:
18+
from jpegio import JpegDecoder
19+
except ImportError:
20+
print("jpegio not supported on this board.")
21+
22+
try:
23+
from io import BufferedReader
24+
from typing import Tuple, Optional
25+
from .displayio_types import BitmapConstructor
26+
except ImportError:
27+
pass
28+
29+
from displayio import Bitmap, ColorConverter, Colorspace
30+
31+
__version__ = "0.0.0+auto.0"
32+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
33+
34+
35+
def load(
36+
file: BufferedReader,
37+
*,
38+
bitmap: BitmapConstructor,
39+
) -> Tuple[Bitmap, Optional[ColorConverter]]:
40+
"""
41+
Loads a JPG image from the open ''file''.
42+
The JPG must be a Baseline JPG, Progressive and Lossless JPG formats are not supported.
43+
44+
Returns tuple of bitmap object and ColorConverter object.
45+
46+
:param io.BufferedReader file: Open file handle or compatible (like 'io.BytesIO')
47+
:param object bitmap: Type to store bitmap data.
48+
Must have API similar to 'displayio.Bitmap'. Will be skipped if None.
49+
Will be skipped if None.
50+
"""
51+
decoder = JpegDecoder()
52+
width, height = decoder.open(file)
53+
bitmap_obj = bitmap(width, height, 65535)
54+
decoder.decode(bitmap_obj)
55+
56+
return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)

examples/imageload_jpg_simpletest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2024 Channing Ramos
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Basic JPG imageload example
7+
"""
8+
9+
import board
10+
import displayio
11+
import adafruit_imageload
12+
13+
group = displayio.Group()
14+
board.DISPLAY.root_group = group
15+
16+
image, color_converter = adafruit_imageload.load("images/jpg_test.jpg")
17+
18+
tile_grid = displayio.TileGrid(image, pixel_shader=color_converter)
19+
group.append(tile_grid)
20+
21+
while True:
22+
pass

examples/images/jpg_test.jpg

5.34 KB
Loading

examples/images/jpg_test.jpg.license

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2024 Channing Ramos
2+
# SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)