Skip to content

adding type annotations and png example script #63

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 1 commit into from
Jan 9, 2023
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
10 changes: 8 additions & 2 deletions adafruit_imageload/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@

"""

try:
# pylint: disable=unused-import
import typing
from .displayio_types import PaletteConstructor, BitmapConstructor
except ImportError:
pass

import struct
import zlib


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


def load(
file, *, bitmap, palette=None
file: str, *, bitmap: BitmapConstructor, palette: PaletteConstructor = None
): # pylint: disable=too-many-locals,too-many-branches
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been researching a little about how to annotate the return type of this correctly, without assuming that we are always going to use the displayio.Bitmap and displayio.Palette. It seems like we can do this by using type aliases (https://peps.python.org/pep-0484/#type-aliases) to make sure the function returns the same type as the constructor that it gets, and optionally maybe protocols (https://peps.python.org/pep-0544/) to make sure those types have the required methods on them.

"""Loads a PNG image from the open ``file``.

Expand Down
43 changes: 43 additions & 0 deletions examples/imageload_png_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: 2022 Tim Cocks
# SPDX-License-Identifier: MIT

import board
import displayio
from vectorio import Rectangle
import adafruit_imageload

# built-in display
display = board.DISPLAY

# load png image
image, palette = adafruit_imageload.load("images/test_image.png")

# Set the transparency index color to be hidden
palette.make_transparent(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another todo – we could read the transparency information from the file, there are two kinds of chunks that can define that.


# make tilegrid for the loaded image
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
tile_grid.x = display.width // 2 - tile_grid.tile_width // 2
tile_grid.y = display.height // 2 - tile_grid.tile_height // 2

# make a blank background
bg_palette = displayio.Palette(1)
bg_palette[0] = 0xFFFFFF
rect = Rectangle(
pixel_shader=bg_palette, width=display.width, height=display.height, x=0, y=0
)

# make a group to show on the display
group = displayio.Group()

# add background
group.append(rect)
# add loaded image tilegrid
group.append(tile_grid)

# show our group
board.DISPLAY.show(group)

# loop forever so it stays on the display
while True:
pass
Binary file added examples/images/test_image.png
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/test_image.png.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: 2022 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT