From d05b73cd08cfd7b40b88f3f1f0f4a03a50fe8161 Mon Sep 17 00:00:00 2001 From: Peter Rowlands Date: Fri, 10 Jan 2020 14:44:47 +0900 Subject: [PATCH] Use context manager for APNG tests --- Tests/test_file_apng.py | 479 ++++++++++++++++++++-------------------- 1 file changed, 242 insertions(+), 237 deletions(-) diff --git a/Tests/test_file_apng.py b/Tests/test_file_apng.py index 8c5ea90ff..36110e891 100644 --- a/Tests/test_file_apng.py +++ b/Tests/test_file_apng.py @@ -11,276 +11,280 @@ class TestFilePng(PillowTestCase): # https://philip.html5.org/tests/apng/tests.html # (referenced from https://wiki.mozilla.org/APNG_Specification) def test_apng_basic(self): - im = Image.open("Tests/images/apng/single_frame.png") - self.assertTrue(im.is_animated) - self.assertEqual(im.get_format_mimetype(), "image/apng") - self.assertIsNone(im.info.get("default_image")) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/single_frame.png") as im: + self.assertTrue(im.is_animated) + self.assertEqual(im.get_format_mimetype(), "image/apng") + self.assertIsNone(im.info.get("default_image")) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/single_frame_default.png") - self.assertTrue(im.is_animated) - self.assertEqual(im.get_format_mimetype(), "image/apng") - self.assertTrue(im.info.get("default_image")) - self.assertEqual(im.getpixel((0, 0)), (255, 0, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (255, 0, 0, 255)) - im.seek(1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/single_frame_default.png") as im: + self.assertTrue(im.is_animated) + self.assertEqual(im.get_format_mimetype(), "image/apng") + self.assertTrue(im.info.get("default_image")) + self.assertEqual(im.getpixel((0, 0)), (255, 0, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (255, 0, 0, 255)) + im.seek(1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - # test out of bounds seek - with self.assertRaises(EOFError): - im.seek(2) + # test out of bounds seek + with self.assertRaises(EOFError): + im.seek(2) - # test rewind support - im.seek(0) - self.assertEqual(im.getpixel((0, 0)), (255, 0, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (255, 0, 0, 255)) - im.seek(1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + # test rewind support + im.seek(0) + self.assertEqual(im.getpixel((0, 0)), (255, 0, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (255, 0, 0, 255)) + im.seek(1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) def test_apng_fdat(self): - im = Image.open("Tests/images/apng/split_fdat.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/split_fdat.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/split_fdat_zero_chunk.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/split_fdat_zero_chunk.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) def test_apng_dispose(self): - im = Image.open("Tests/images/apng/dispose_op_none.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_none.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/dispose_op_background.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) + with Image.open("Tests/images/apng/dispose_op_background.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) - im = Image.open("Tests/images/apng/dispose_op_background_final.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_background_final.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/dispose_op_previous.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_previous.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/dispose_op_previous_final.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_previous_final.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/dispose_op_previous_first.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) + with Image.open("Tests/images/apng/dispose_op_previous_first.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) def test_apng_dispose_region(self): - im = Image.open("Tests/images/apng/dispose_op_none_region.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_none_region.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/dispose_op_background_before_region.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) + with Image.open( + "Tests/images/apng/dispose_op_background_before_region.png" + ) as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) - im = Image.open("Tests/images/apng/dispose_op_background_region.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 255, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) + with Image.open("Tests/images/apng/dispose_op_background_region.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 255, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) - im = Image.open("Tests/images/apng/dispose_op_previous_region.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/dispose_op_previous_region.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) def test_apng_blend(self): - im = Image.open("Tests/images/apng/blend_op_source_solid.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/blend_op_source_solid.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/blend_op_source_transparent.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) + with Image.open("Tests/images/apng/blend_op_source_transparent.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 0, 0)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 0, 0)) - im = Image.open("Tests/images/apng/blend_op_source_near_transparent.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 2)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 2)) + with Image.open("Tests/images/apng/blend_op_source_near_transparent.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 2)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 2)) - im = Image.open("Tests/images/apng/blend_op_over.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/blend_op_over.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/blend_op_over_near_transparent.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 97)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/blend_op_over_near_transparent.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 97)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) def test_apng_chunk_order(self): - im = Image.open("Tests/images/apng/fctl_actl.png") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/fctl_actl.png") as im: + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) def test_apng_delay(self): - im = Image.open("Tests/images/apng/delay.png") - im.seek(1) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(2) - self.assertEqual(im.info.get("duration"), 1000.0) - im.seek(3) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(4) - self.assertEqual(im.info.get("duration"), 1000.0) + with Image.open("Tests/images/apng/delay.png") as im: + im.seek(1) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(2) + self.assertEqual(im.info.get("duration"), 1000.0) + im.seek(3) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(4) + self.assertEqual(im.info.get("duration"), 1000.0) - im = Image.open("Tests/images/apng/delay_round.png") - im.seek(1) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(2) - self.assertEqual(im.info.get("duration"), 1000.0) + with Image.open("Tests/images/apng/delay_round.png") as im: + im.seek(1) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(2) + self.assertEqual(im.info.get("duration"), 1000.0) - im = Image.open("Tests/images/apng/delay_short_max.png") - im.seek(1) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(2) - self.assertEqual(im.info.get("duration"), 1000.0) + with Image.open("Tests/images/apng/delay_short_max.png") as im: + im.seek(1) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(2) + self.assertEqual(im.info.get("duration"), 1000.0) - im = Image.open("Tests/images/apng/delay_zero_denom.png") - im.seek(1) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(2) - self.assertEqual(im.info.get("duration"), 1000.0) + with Image.open("Tests/images/apng/delay_zero_denom.png") as im: + im.seek(1) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(2) + self.assertEqual(im.info.get("duration"), 1000.0) - im = Image.open("Tests/images/apng/delay_zero_numer.png") - im.seek(1) - self.assertEqual(im.info.get("duration"), 0.0) - im.seek(2) - self.assertEqual(im.info.get("duration"), 0.0) - im.seek(3) - self.assertEqual(im.info.get("duration"), 500.0) - im.seek(4) - self.assertEqual(im.info.get("duration"), 1000.0) + with Image.open("Tests/images/apng/delay_zero_numer.png") as im: + im.seek(1) + self.assertEqual(im.info.get("duration"), 0.0) + im.seek(2) + self.assertEqual(im.info.get("duration"), 0.0) + im.seek(3) + self.assertEqual(im.info.get("duration"), 500.0) + im.seek(4) + self.assertEqual(im.info.get("duration"), 1000.0) def test_apng_num_plays(self): - im = Image.open("Tests/images/apng/num_plays.png") - self.assertEqual(im.info.get("loop"), 0) + with Image.open("Tests/images/apng/num_plays.png") as im: + self.assertEqual(im.info.get("loop"), 0) - im = Image.open("Tests/images/apng/num_plays_1.png") - self.assertEqual(im.info.get("loop"), 1) + with Image.open("Tests/images/apng/num_plays_1.png") as im: + self.assertEqual(im.info.get("loop"), 1) def test_apng_mode(self): - im = Image.open("Tests/images/apng/mode_16bit.png") - self.assertEqual(im.mode, "RGBA") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (0, 0, 128, 191)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 128, 191)) + with Image.open("Tests/images/apng/mode_16bit.png") as im: + self.assertEqual(im.mode, "RGBA") + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (0, 0, 128, 191)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 128, 191)) - im = Image.open("Tests/images/apng/mode_greyscale.png") - self.assertEqual(im.mode, "L") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), 128) - self.assertEqual(im.getpixel((64, 32)), 255) + with Image.open("Tests/images/apng/mode_greyscale.png") as im: + self.assertEqual(im.mode, "L") + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), 128) + self.assertEqual(im.getpixel((64, 32)), 255) - im = Image.open("Tests/images/apng/mode_greyscale_alpha.png") - self.assertEqual(im.mode, "LA") - im.seek(im.n_frames - 1) - self.assertEqual(im.getpixel((0, 0)), (128, 191)) - self.assertEqual(im.getpixel((64, 32)), (128, 191)) + with Image.open("Tests/images/apng/mode_greyscale_alpha.png") as im: + self.assertEqual(im.mode, "LA") + im.seek(im.n_frames - 1) + self.assertEqual(im.getpixel((0, 0)), (128, 191)) + self.assertEqual(im.getpixel((64, 32)), (128, 191)) - im = Image.open("Tests/images/apng/mode_palette.png") - self.assertEqual(im.mode, "P") - im.seek(im.n_frames - 1) - im = im.convert("RGB") - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0)) + with Image.open("Tests/images/apng/mode_palette.png") as im: + self.assertEqual(im.mode, "P") + im.seek(im.n_frames - 1) + im = im.convert("RGB") + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0)) - im = Image.open("Tests/images/apng/mode_palette_alpha.png") - self.assertEqual(im.mode, "P") - im.seek(im.n_frames - 1) - im = im.convert("RGBA") - self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) - self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) + with Image.open("Tests/images/apng/mode_palette_alpha.png") as im: + self.assertEqual(im.mode, "P") + im.seek(im.n_frames - 1) + im = im.convert("RGBA") + self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) + self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") - self.assertEqual(im.mode, "P") - im.seek(im.n_frames - 1) - im = im.convert("RGBA") - self.assertEqual(im.getpixel((0, 0)), (0, 0, 255, 128)) - self.assertEqual(im.getpixel((64, 32)), (0, 0, 255, 128)) + with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im: + self.assertEqual(im.mode, "P") + im.seek(im.n_frames - 1) + im = im.convert("RGBA") + self.assertEqual(im.getpixel((0, 0)), (0, 0, 255, 128)) + self.assertEqual(im.getpixel((64, 32)), (0, 0, 255, 128)) def test_apng_chunk_errors(self): - im = Image.open("Tests/images/apng/chunk_no_actl.png") - self.assertFalse(im.is_animated) + with Image.open("Tests/images/apng/chunk_no_actl.png") as im: + self.assertFalse(im.is_animated) def open(): - im = Image.open("Tests/images/apng/chunk_multi_actl.png") - im.load() + with Image.open("Tests/images/apng/chunk_multi_actl.png") as im: + im.load() + self.assertFalse(im.is_animated) pytest.warns(UserWarning, open) - self.assertFalse(im.is_animated) - im = Image.open("Tests/images/apng/chunk_actl_after_idat.png") - self.assertFalse(im.is_animated) + with Image.open("Tests/images/apng/chunk_actl_after_idat.png") as im: + self.assertFalse(im.is_animated) - im = Image.open("Tests/images/apng/chunk_no_fctl.png") - with self.assertRaises(SyntaxError): - im.seek(im.n_frames - 1) + with Image.open("Tests/images/apng/chunk_no_fctl.png") as im: + with self.assertRaises(SyntaxError): + im.seek(im.n_frames - 1) - im = Image.open("Tests/images/apng/chunk_repeat_fctl.png") - with self.assertRaises(SyntaxError): - im.seek(im.n_frames - 1) + with Image.open("Tests/images/apng/chunk_repeat_fctl.png") as im: + with self.assertRaises(SyntaxError): + im.seek(im.n_frames - 1) - im = Image.open("Tests/images/apng/chunk_no_fdat.png") - with self.assertRaises(SyntaxError): - im.seek(im.n_frames - 1) + with Image.open("Tests/images/apng/chunk_no_fdat.png") as im: + with self.assertRaises(SyntaxError): + im.seek(im.n_frames - 1) def test_apng_syntax_errors(self): - def open(): - im = Image.open("Tests/images/apng/syntax_num_frames_zero.png") - self.assertFalse(im.is_animated) - with self.assertRaises(OSError): + def open_frames_zero(): + with Image.open("Tests/images/apng/syntax_num_frames_zero.png") as im: + self.assertFalse(im.is_animated) + with self.assertRaises(OSError): + im.load() + + pytest.warns(UserWarning, open_frames_zero) + + def open_frames_zero_default(): + with Image.open( + "Tests/images/apng/syntax_num_frames_zero_default.png" + ) as im: + self.assertFalse(im.is_animated) im.load() - pytest.warns(UserWarning, open) - - def open(): - im = Image.open("Tests/images/apng/syntax_num_frames_zero_default.png") - self.assertFalse(im.is_animated) - im.load() - - pytest.warns(UserWarning, open) + pytest.warns(UserWarning, open_frames_zero_default) # we can handle this case gracefully exception = None - im = Image.open("Tests/images/apng/syntax_num_frames_low.png") - try: - im.seek(im.n_frames - 1) - except Exception as e: - exception = e - self.assertIsNone(exception) + with Image.open("Tests/images/apng/syntax_num_frames_low.png") as im: + try: + im.seek(im.n_frames - 1) + except Exception as e: + exception = e + self.assertIsNone(exception) with self.assertRaises(SyntaxError): - im = Image.open("Tests/images/apng/syntax_num_frames_high.png") - im.seek(im.n_frames - 1) - im.load() + with Image.open("Tests/images/apng/syntax_num_frames_high.png") as im: + im.seek(im.n_frames - 1) + im.load() def open(): - im = Image.open("Tests/images/apng/syntax_num_frames_invalid.png") - self.assertFalse(im.is_animated) - im.load() + with Image.open("Tests/images/apng/syntax_num_frames_invalid.png") as im: + self.assertFalse(im.is_animated) + im.load() pytest.warns(UserWarning, open) @@ -296,14 +300,14 @@ class TestFilePng(PillowTestCase): ] for f in test_files: with self.assertRaises(SyntaxError): - im = Image.open("Tests/images/apng/{0}".format(f)) - im.seek(im.n_frames - 1) - im.load() + with Image.open("Tests/images/apng/{0}".format(f)) as im: + im.seek(im.n_frames - 1) + im.load() def test_apng_save(self): - im = Image.open("Tests/images/apng/single_frame.png") - test_file = self.tempfile("temp.png") - im.save(test_file, save_all=True) + with Image.open("Tests/images/apng/single_frame.png") as im: + test_file = self.tempfile("temp.png") + im.save(test_file, save_all=True) with Image.open(test_file) as im: im.load() @@ -313,13 +317,13 @@ class TestFilePng(PillowTestCase): self.assertEqual(im.getpixel((0, 0)), (0, 255, 0, 255)) self.assertEqual(im.getpixel((64, 32)), (0, 255, 0, 255)) - im = Image.open("Tests/images/apng/single_frame_default.png") - frames = [] - for im in ImageSequence.Iterator(im): - frames.append(im.copy()) - frames[0].save( - test_file, save_all=True, default_image=True, append_images=frames[1:] - ) + with Image.open("Tests/images/apng/single_frame_default.png") as im: + frames = [] + for frame_im in ImageSequence.Iterator(im): + frames.append(frame_im.copy()) + frames[0].save( + test_file, save_all=True, default_image=True, append_images=frames[1:] + ) with Image.open(test_file) as im: im.load() @@ -352,23 +356,24 @@ class TestFilePng(PillowTestCase): def test_apng_save_duration_loop(self): test_file = self.tempfile("temp.png") - im = Image.open("Tests/images/apng/delay.png") - frames = [] - durations = [] - loop = im.info.get("loop") - default_image = im.info.get("default_image") - for i, im in enumerate(ImageSequence.Iterator(im)): - frames.append(im.copy()) - if i != 0 or not default_image: - durations.append(im.info.get("duration", 0)) - frames[0].save( - test_file, - save_all=True, - default_image=default_image, - append_images=frames[1:], - duration=durations, - loop=loop, - ) + with Image.open("Tests/images/apng/delay.png") as im: + frames = [] + durations = [] + loop = im.info.get("loop") + default_image = im.info.get("default_image") + for i, frame_im in enumerate(ImageSequence.Iterator(im)): + frames.append(frame_im.copy()) + if i != 0 or not default_image: + durations.append(frame_im.info.get("duration", 0)) + frames[0].save( + test_file, + save_all=True, + default_image=default_image, + append_images=frames[1:], + duration=durations, + loop=loop, + ) + with Image.open(test_file) as im: im.load() self.assertEqual(im.info.get("loop"), loop)