Skip to content

Commit 0e2c512

Browse files
committed
JPG Reading Implemented
Added a JPG module that calls jpegio and updated the base __init__.py to recognize JPG files.
1 parent 30f3e71 commit 0e2c512

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

adafruit_imageload/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def load(
6868
with open_file as file:
6969
header = file.read(3)
7070
file.seek(0)
71+
print(header)
7172
if header.startswith(b"BM"):
7273
from . import bmp
7374

@@ -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, palette=palette)
9297
raise RuntimeError("Unsupported image format")

adafruit_imageload/jpg.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 jpegio.
10+
11+
* Author(s): Channing Ramos
12+
13+
"""
14+
15+
#A separate try for jpegio. While it has wide support it is not universal, 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, Iterator, Optional, List
25+
from .displayio_types import PaletteConstructor, BitmapConstructor
26+
except ImportError:
27+
pass
28+
29+
from displayio import Bitmap, ColorConverter, Colorspace
30+
31+
def load(file: BufferedReader,
32+
*,
33+
bitmap: BitmapConstructor,
34+
palette: Optional[PaletteConstructor] = None) -> Tuple[Bitmap, Optional[ColorConverter]]:
35+
36+
decoder = JpegDecoder()
37+
width, height = decoder.open(file)
38+
bitmap_obj = bitmap(width, height, 65535)
39+
decoder.decode(bitmap_obj)
40+
41+
return bitmap_obj, ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)

0 commit comments

Comments
 (0)