Skip to content

Commit faa843e

Browse files
authored
Simplify WebP code (#9329)
1 parent 66e3d65 commit faa843e

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/PIL/WebPImagePlugin.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,30 @@ class WebPImageFile(ImageFile.ImageFile):
4545
def _open(self) -> None:
4646
# Use the newer AnimDecoder API to parse the (possibly) animated file,
4747
# and access muxed chunks like ICC/EXIF/XMP.
48+
assert self.fp is not None
4849
self._decoder = _webp.WebPAnimDecoder(self.fp.read())
4950

5051
# Get info from decoder
51-
self._size, loop_count, bgcolor, frame_count, mode = self._decoder.get_info()
52-
self.info["loop"] = loop_count
53-
bg_a, bg_r, bg_g, bg_b = (
54-
(bgcolor >> 24) & 0xFF,
55-
(bgcolor >> 16) & 0xFF,
56-
(bgcolor >> 8) & 0xFF,
57-
bgcolor & 0xFF,
52+
self._size, self.info["loop"], bgcolor, self.n_frames, self.rawmode = (
53+
self._decoder.get_info()
54+
)
55+
self.info["background"] = (
56+
(bgcolor >> 16) & 0xFF, # R
57+
(bgcolor >> 8) & 0xFF, # G
58+
bgcolor & 0xFF, # B
59+
(bgcolor >> 24) & 0xFF, # A
5860
)
59-
self.info["background"] = (bg_r, bg_g, bg_b, bg_a)
60-
self.n_frames = frame_count
6161
self.is_animated = self.n_frames > 1
62-
self._mode = "RGB" if mode == "RGBX" else mode
63-
self.rawmode = mode
62+
self._mode = "RGB" if self.rawmode == "RGBX" else self.rawmode
6463

6564
# Attempt to read ICC / EXIF / XMP chunks from file
66-
icc_profile = self._decoder.get_chunk("ICCP")
67-
exif = self._decoder.get_chunk("EXIF")
68-
xmp = self._decoder.get_chunk("XMP ")
69-
if icc_profile:
70-
self.info["icc_profile"] = icc_profile
71-
if exif:
72-
self.info["exif"] = exif
73-
if xmp:
74-
self.info["xmp"] = xmp
65+
for key, chunk_name in {
66+
"icc_profile": "ICCP",
67+
"exif": "EXIF",
68+
"xmp": "XMP ",
69+
}.items():
70+
if value := self._decoder.get_chunk(chunk_name):
71+
self.info[key] = value
7572

7673
# Initialize seek state
7774
self._reset(reset=False)
@@ -129,9 +126,7 @@ def load(self) -> Image.core.PixelAccess | None:
129126
self._seek(self.__logical_frame)
130127

131128
# We need to load the image data for this frame
132-
data, timestamp, duration = self._get_next()
133-
self.info["timestamp"] = timestamp
134-
self.info["duration"] = duration
129+
data, self.info["timestamp"], self.info["duration"] = self._get_next()
135130
self.__loaded = self.__logical_frame
136131

137132
# Set tile

0 commit comments

Comments
 (0)