Skip to content

Png transparency #82

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 5 commits into from
Aug 2, 2024
Merged
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
13 changes: 11 additions & 2 deletions adafruit_imageload/png.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: 2022 Radomir Dopieralski
# SPDX-FileCopyrightText: 2023 Matt Land
# SPDX-FileCopyrightText: 2024 Channing Ramos
#
# SPDX-License-Identifier: MIT

Expand All @@ -10,7 +11,7 @@
Load pixel values (indices or colors) into a bitmap and colors into a palette
from a PNG file.

* Author(s): Radomir Dopieralski, Matt Land
* Author(s): Radomir Dopieralski, Matt Land, Channing Ramos

"""

Expand Down Expand Up @@ -48,7 +49,7 @@ def load(
:param object palette: Type to store the palette. Must have API similar to
`displayio.Palette`. Will be skipped if None.
"""
# pylint: disable=too-many-locals,too-many-branches
# pylint: disable=too-many-locals,too-many-branches, consider-using-enumerate, too-many-statements
header = file.read(8)
if header != b"\x89PNG\r\n\x1a\n":
raise ValueError("Not a PNG file")
Expand Down Expand Up @@ -86,6 +87,14 @@ def load(
pal = palette(pal_size)
for i in range(pal_size):
pal[i] = file.read(3)
elif chunk == b"tRNS":
if size > len(pal):
raise ValueError("More transparency entries than palette entries")
trns_data = file.read(size)
for i in range(len(trns_data)):
if trns_data[i] == 0:
pal.make_transparent(i)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we del trns_data once we are done with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure can! Latest commit does so now.

del trns_data
elif chunk == b"IDAT":
data.extend(file.read(size))
elif chunk == b"IEND":
Expand Down