Skip to content

Commit b3953c8

Browse files
authored
Merge pull request #63 from FoamyGuy/png_typing_and_example
adding type annotations and png example script
2 parents 9b00824 + 5931103 commit b3953c8

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

adafruit_imageload/png.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@
1313
1414
"""
1515

16+
try:
17+
# pylint: disable=unused-import
18+
import typing
19+
from .displayio_types import PaletteConstructor, BitmapConstructor
20+
except ImportError:
21+
pass
22+
1623
import struct
1724
import zlib
1825

19-
2026
__version__ = "0.0.0-auto.0"
2127
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
2228

2329

2430
def load(
25-
file, *, bitmap, palette=None
31+
file: str, *, bitmap: BitmapConstructor, palette: PaletteConstructor = None
2632
): # pylint: disable=too-many-locals,too-many-branches
2733
"""Loads a PNG image from the open ``file``.
2834

examples/imageload_png_simpletest.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import displayio
6+
from vectorio import Rectangle
7+
import adafruit_imageload
8+
9+
# built-in display
10+
display = board.DISPLAY
11+
12+
# load png image
13+
image, palette = adafruit_imageload.load("images/test_image.png")
14+
15+
# Set the transparency index color to be hidden
16+
palette.make_transparent(0)
17+
18+
# make tilegrid for the loaded image
19+
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
20+
tile_grid.x = display.width // 2 - tile_grid.tile_width // 2
21+
tile_grid.y = display.height // 2 - tile_grid.tile_height // 2
22+
23+
# make a blank background
24+
bg_palette = displayio.Palette(1)
25+
bg_palette[0] = 0xFFFFFF
26+
rect = Rectangle(
27+
pixel_shader=bg_palette, width=display.width, height=display.height, x=0, y=0
28+
)
29+
30+
# make a group to show on the display
31+
group = displayio.Group()
32+
33+
# add background
34+
group.append(rect)
35+
# add loaded image tilegrid
36+
group.append(tile_grid)
37+
38+
# show our group
39+
board.DISPLAY.show(group)
40+
41+
# loop forever so it stays on the display
42+
while True:
43+
pass

examples/images/test_image.png

5.86 KB
Loading
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT

0 commit comments

Comments
 (0)