Skip to content

Commit aa4ca21

Browse files
committed
Simplify code
1 parent eeaadf8 commit aa4ca21

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

‎Tests/test_file_apng.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,23 +686,18 @@ def test_apng_save_disposal_previous(tmp_path: Path) -> None:
686686
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
687687

688688

689-
@pytest.mark.parametrize(
690-
"disposal",
691-
(PngImagePlugin.Disposal.OP_BACKGROUND, PngImagePlugin.Disposal.OP_PREVIOUS),
692-
)
693-
def test_apng_save_info_disposal(tmp_path: Path, disposal: int) -> None:
689+
def test_apng_save_info_disposal(tmp_path: Path) -> None:
694690
test_file = tmp_path / "temp.png"
695-
red = Image.new("RGBA", (128, 64), (255, 0, 0, 255))
691+
red = Image.new("RGBA", (32, 32), (255, 0, 0, 255))
696692
red_green = red.copy()
697693
red_green.paste((0, 255, 0, 255), (0, 0, 10, 10))
698694

699-
# disposal from info should give the same result as disposal as an argument
700-
red.info["disposal"] = disposal
695+
red.info["disposal"] = PngImagePlugin.Disposal.OP_PREVIOUS
701696
red.save(test_file, save_all=True, append_images=[red_green])
702697
with Image.open(test_file) as im:
703698
im.seek(1)
704699
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
705-
assert im.getpixel((64, 32)) == (255, 0, 0, 255)
700+
assert im.getpixel((20, 20)) == (255, 0, 0, 255)
706701

707702

708703
def test_apng_save_blend(tmp_path: Path) -> None:

‎src/PIL/PngImagePlugin.py‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,16 +1225,20 @@ def _write_multiple_frames(
12251225
encoderinfo["duration"] = duration[frame_count]
12261226
elif duration is None and "duration" in im_frame.info:
12271227
encoderinfo["duration"] = im_frame.info["duration"]
1228-
if isinstance(disposal, (list, tuple)):
1229-
encoderinfo["disposal"] = disposal[frame_count]
1230-
if isinstance(blend, (list, tuple)):
1231-
encoderinfo["blend"] = blend[frame_count]
1228+
encoderinfo["disposal"] = (
1229+
disposal[frame_count]
1230+
if isinstance(disposal, (list, tuple))
1231+
else disposal
1232+
)
1233+
encoderinfo["blend"] = (
1234+
blend[frame_count] if isinstance(blend, (list, tuple)) else blend
1235+
)
12321236
frame_count += 1
12331237

12341238
if im_frames:
12351239
previous = im_frames[-1]
1236-
prev_disposal = previous.encoderinfo.get("disposal", disposal)
1237-
prev_blend = previous.encoderinfo.get("blend", blend)
1240+
prev_disposal = previous.encoderinfo["disposal"]
1241+
prev_blend = previous.encoderinfo["blend"]
12381242
if prev_disposal == Disposal.OP_PREVIOUS and len(im_frames) < 2:
12391243
prev_disposal = Disposal.OP_BACKGROUND
12401244

@@ -1257,8 +1261,8 @@ def _write_multiple_frames(
12571261
bbox = delta.getbbox(alpha_only=False)
12581262
if (
12591263
not bbox
1260-
and prev_disposal == encoderinfo.get("disposal", disposal)
1261-
and prev_blend == encoderinfo.get("blend", blend)
1264+
and prev_disposal == encoderinfo["disposal"]
1265+
and prev_blend == encoderinfo["blend"]
12621266
and "duration" in encoderinfo
12631267
):
12641268
previous.encoderinfo["duration"] += encoderinfo["duration"]
@@ -1303,8 +1307,8 @@ def _write_multiple_frames(
13031307
if delay.numerator > 65535:
13041308
msg = "cannot write duration"
13051309
raise ValueError(msg)
1306-
frame_disposal = encoderinfo.get("disposal", disposal)
1307-
frame_blend = encoderinfo.get("blend", blend)
1310+
frame_disposal = encoderinfo["disposal"]
1311+
frame_blend = encoderinfo["blend"]
13081312
# frame control
13091313
chunk(
13101314
fp,

0 commit comments

Comments
 (0)