From 35475914b9f49dab0d23a9082a735c0f0a68ab2e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 Sep 2022 18:17:44 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Tests/test_file_ani.py | 91 +++++++++++++++++++++++++++------------ Tests/test_file_cur.py | 43 ++++++++++++------ src/PIL/AniImagePlugin.py | 4 +- 3 files changed, 95 insertions(+), 43 deletions(-) diff --git a/Tests/test_file_ani.py b/Tests/test_file_ani.py index cd7703164..9d5bfd649 100644 --- a/Tests/test_file_ani.py +++ b/Tests/test_file_ani.py @@ -1,5 +1,7 @@ from io import BytesIO + import pytest + from PIL import Image @@ -65,55 +67,72 @@ def test_stopwtch(): def test_save(): directory_path = "Tests/images/ani/" - filenames = ["aero_busy_0.png", "aero_busy_8.png", - "posy_busy_0.png", "posy_busy_24.png", - "stopwtch_0.png", "stopwtch_5.png"] + filenames = [ + "aero_busy_0.png", + "aero_busy_8.png", + "posy_busy_0.png", + "posy_busy_24.png", + "stopwtch_0.png", + "stopwtch_5.png", + ] images = [Image.open(directory_path + filename) for filename in filenames] with BytesIO() as output: - images[0].save(output, append_images=[images[1]], seq=[ - 0, 1], rate=[5, 10], format="ANI") + images[0].save( + output, append_images=[images[1]], seq=[0, 1], rate=[5, 10], format="ANI" + ) with Image.open(output, formats=["ANI"]) as im: assert im.tobytes() == images[0].tobytes() im.seek(1) assert im.tobytes() == images[1].tobytes() - assert im.info['seq'] == [0, 1] - assert im.info['rate'] == [5, 10] + assert im.info["seq"] == [0, 1] + assert im.info["rate"] == [5, 10] with BytesIO() as output: - images[2].save(output, append_images=[images[3]], seq=[ - 1, 0], rate=[2, 2], format="ANI", sizes=[(96, 96)]) + images[2].save( + output, + append_images=[images[3]], + seq=[1, 0], + rate=[2, 2], + format="ANI", + sizes=[(96, 96)], + ) with Image.open(output, formats=["ANI"]) as im: assert im.tobytes() == images[2].tobytes() im.seek(1) assert im.tobytes() == images[3].tobytes() - assert im.info['seq'] == [1, 0] - assert im.info['rate'] == [2, 2] + assert im.info["seq"] == [1, 0] + assert im.info["rate"] == [2, 2] with BytesIO() as output: - images[4].save(output, append_images=[images[5]], seq=[ - 0, 1], rate=[3, 4], format="ANI") + images[4].save( + output, append_images=[images[5]], seq=[0, 1], rate=[3, 4], format="ANI" + ) with Image.open(output, formats=["ANI"]) as im: assert im.tobytes() == images[4].tobytes() im.seek(1) assert im.tobytes() == images[5].tobytes() - assert im.info['seq'] == [0, 1] - assert im.info['rate'] == [3, 4] + assert im.info["seq"] == [0, 1] + assert im.info["rate"] == [3, 4] with BytesIO() as output: - images[0].save(output, append_images=images[1:], - seq=[0, 2, 4, 1, 3, 5, 0, 1, 0, 1], - rate=[1, 2, 3, 1, 2, 3, 1, 2, 3, 4], - format="ANI", sizes=[(32, 32)]) + images[0].save( + output, + append_images=images[1:], + seq=[0, 2, 4, 1, 3, 5, 0, 1, 0, 1], + rate=[1, 2, 3, 1, 2, 3, 1, 2, 3, 4], + format="ANI", + sizes=[(32, 32)], + ) with Image.open(output, formats=["ANI"]) as im: - assert im.info['frames'] == 6 - assert im.info['seq'] == [0, 2, 4, 1, 3, 5, 0, 1, 0, 1] - assert im.info['rate'] == [1, 2, 3, 1, 2, 3, 1, 2, 3, 4] + assert im.info["frames"] == 6 + assert im.info["seq"] == [0, 2, 4, 1, 3, 5, 0, 1, 0, 1] + assert im.info["rate"] == [1, 2, 3, 1, 2, 3, 1, 2, 3, 4] assert im.size == (32, 32) im.seek(4) @@ -121,12 +140,30 @@ def test_save(): with BytesIO() as output: with pytest.raises(ValueError): - images[0].save(output, append_images=images[1:], seq=[ - 0, 1, 8, 1, 2], rate=[1, 1, 1, 1, 1], format="ANI", sizes=[(32, 32)]) + images[0].save( + output, + append_images=images[1:], + seq=[0, 1, 8, 1, 2], + rate=[1, 1, 1, 1, 1], + format="ANI", + sizes=[(32, 32)], + ) with pytest.raises(ValueError): - images[0].save(output, append_images=images[1:], seq=[ - 0, 1, 1, 1, 2], rate=[1, 1, 1, 1], format="ANI", sizes=[(32, 32)]) + images[0].save( + output, + append_images=images[1:], + seq=[0, 1, 1, 1, 2], + rate=[1, 1, 1, 1], + format="ANI", + sizes=[(32, 32)], + ) with pytest.raises(ValueError): - images[0].save(output, append_images=images[1:], rate=[1, 1, 1, 1], format="ANI", sizes=[(32, 32)]) + images[0].save( + output, + append_images=images[1:], + rate=[1, 1, 1, 1], + format="ANI", + sizes=[(32, 32)], + ) diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index 07eae8cad..165cbb09b 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -1,13 +1,14 @@ +from io import BytesIO + import pytest -from io import BytesIO from PIL import CurImagePlugin, Image def test_deerstalker(): with Image.open("Tests/images/cur/deerstalker.cur") as im: assert im.size == (32, 32) - assert im.info['hotspots'] == [(0, 0)] + assert im.info["hotspots"] == [(0, 0)] assert isinstance(im, CurImagePlugin.CurImageFile) # Check some pixel colors to ensure image is loaded properly assert im.getpixel((10, 1)) == (0, 0, 0, 0) @@ -18,8 +19,8 @@ def test_deerstalker(): def test_posy_link(): with Image.open("Tests/images/cur/posy_link.cur") as im: assert im.size == (128, 128) - assert im.info['sizes'] == [(128, 128), (96, 96), (64, 64), (48, 48), (32, 32)] - assert im.info['hotspots'] == [(25, 7), (18, 5), (12, 3), (9, 2), (5, 1)] + assert im.info["sizes"] == [(128, 128), (96, 96), (64, 64), (48, 48), (32, 32)] + assert im.info["hotspots"] == [(25, 7), (18, 5), (12, 3), (9, 2), (5, 1)] # check some pixel colors assert im.getpixel((0, 0)) == (0, 0, 0, 0) assert im.getpixel((20, 20)) == (0, 0, 0, 255) @@ -69,8 +70,13 @@ def test_save_win98_arrow(): with Image.open("Tests/images/cur/win98_arrow.png") as im: # save the data with BytesIO() as output: - im.save(output, format="CUR", sizes=[(32, 32)], hotspots=[ - (10, 10)], bitmap_format="bmp") + im.save( + output, + format="CUR", + sizes=[(32, 32)], + hotspots=[(10, 10)], + bitmap_format="bmp", + ) with Image.open(output) as im2: assert im.tobytes() == im2.tobytes() @@ -92,27 +98,36 @@ def test_save_posy_link(): with Image.open("Tests/images/cur/posy_link.png") as im: # save the data with BytesIO() as output: - im.save(output, sizes=sizes, hotspots=hotspots, - format="CUR", bitmap_format="bmp") + im.save( + output, + sizes=sizes, + hotspots=hotspots, + format="CUR", + bitmap_format="bmp", + ) # make sure saved output is readable # and sizes/hotspots are correct with Image.open(output, formats=["CUR"]) as im2: assert (128, 128) == im2.size - assert sizes == im2.info['sizes'] + assert sizes == im2.info["sizes"] with BytesIO() as output: - im.save(output, sizes=sizes[3:], hotspots=hotspots[3:], - format="CUR") + im.save(output, sizes=sizes[3:], hotspots=hotspots[3:], format="CUR") # make sure saved output is readable # and sizes/hotspots are correct with Image.open(output, formats=["CUR"]) as im2: assert (48, 48) == im2.size - assert sizes[3:] == im2.info['sizes'] + assert sizes[3:] == im2.info["sizes"] # make sure error is thrown when size and hotspot len's # don't match with pytest.raises(ValueError): - im.save(output, sizes=sizes[2:], hotspots=hotspots[3:], - format="CUR", bitmap_format="bmp") + im.save( + output, + sizes=sizes[2:], + hotspots=hotspots[3:], + format="CUR", + bitmap_format="bmp", + ) diff --git a/src/PIL/AniImagePlugin.py b/src/PIL/AniImagePlugin.py index d4ec37da4..72f4543f9 100644 --- a/src/PIL/AniImagePlugin.py +++ b/src/PIL/AniImagePlugin.py @@ -71,7 +71,7 @@ def _save_frame(im: Image.Image, fp: BytesIO, filename: str, info: dict): image_bytes = image_io.read() if bmp: image_bytes = image_bytes[:8] + o32(height * 2) + image_bytes[12:] - + bytes_len = len(image_bytes) fp.write(o32(bytes_len)) # dwBytesInRes(4) fp.write(o32(offset)) # dwImageOffset(4) @@ -415,7 +415,7 @@ class AniImageFile(ImageFile.ImageFile): self.mode = im.mode def seek(self, frame): - + if frame > self.info["frames"] - 1 or frame < 0: raise EOFError("Frame index out of animation bounds")