Skip to content

Commit 7a68dcd

Browse files
authored
Merge pull request #82 from ch4nsuk3/png-transparency
Png transparency
2 parents 6b7deeb + a7ac0db commit 7a68dcd

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

adafruit_imageload/png.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-FileCopyrightText: 2022 Radomir Dopieralski
22
# SPDX-FileCopyrightText: 2023 Matt Land
3+
# SPDX-FileCopyrightText: 2024 Channing Ramos
34
#
45
# SPDX-License-Identifier: MIT
56

@@ -10,7 +11,7 @@
1011
Load pixel values (indices or colors) into a bitmap and colors into a palette
1112
from a PNG file.
1213
13-
* Author(s): Radomir Dopieralski, Matt Land
14+
* Author(s): Radomir Dopieralski, Matt Land, Channing Ramos
1415
1516
"""
1617

@@ -48,7 +49,7 @@ def load(
4849
:param object palette: Type to store the palette. Must have API similar to
4950
`displayio.Palette`. Will be skipped if None.
5051
"""
51-
# pylint: disable=too-many-locals,too-many-branches
52+
# pylint: disable=too-many-locals,too-many-branches, consider-using-enumerate, too-many-statements
5253
header = file.read(8)
5354
if header != b"\x89PNG\r\n\x1a\n":
5455
raise ValueError("Not a PNG file")
@@ -86,6 +87,14 @@ def load(
8687
pal = palette(pal_size)
8788
for i in range(pal_size):
8889
pal[i] = file.read(3)
90+
elif chunk == b"tRNS":
91+
if size > len(pal):
92+
raise ValueError("More transparency entries than palette entries")
93+
trns_data = file.read(size)
94+
for i in range(len(trns_data)):
95+
if trns_data[i] == 0:
96+
pal.make_transparent(i)
97+
del trns_data
8998
elif chunk == b"IDAT":
9099
data.extend(file.read(size))
91100
elif chunk == b"IEND":

0 commit comments

Comments
 (0)