From fd80b2e1d970e7ff115ce9a13b4311a7127c0da1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 18:46:09 +1100 Subject: [PATCH 1/5] Moved sample data inside test --- Tests/test_image.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 1c2b6d4b938..23f6b86deaa 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1045,10 +1045,6 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: class TestImageBytes: - sample_bytes = bytes( - range(2 * 2 * max(pixelsize for mode, num_bands, pixelsize in image_modes)) - ) - @pytest.mark.parametrize("mode", image_mode_names) def test_roundtrip_bytes_constructor(self, mode: str): source_image = hopper(mode) @@ -1065,11 +1061,11 @@ def test_roundtrip_bytes_method(self, mode: str): assert copy_image.tobytes() == source_bytes @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes) - def test_pixels_after_getdata_putdata( + def test_getdata_putdata( self, mode: str, num_bands: int, pixelsize: int ): image_byte_size = 2 * 2 * pixelsize - start_bytes = self.sample_bytes[:image_byte_size] + start_bytes = bytes(range(image_byte_size)) image = Image.frombytes(mode, (2, 2), start_bytes) start_pixels = ( From c3997050b02ddd552e0b01062ffa89131819aa0a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 19:11:19 +1100 Subject: [PATCH 2/5] Simplified test using assert_image_equal --- Tests/test_image.py | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 23f6b86deaa..d1817d9bfaf 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1047,44 +1047,31 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: class TestImageBytes: @pytest.mark.parametrize("mode", image_mode_names) def test_roundtrip_bytes_constructor(self, mode: str): - source_image = hopper(mode) - source_bytes = source_image.tobytes() - copy_image = Image.frombytes(mode, source_image.size, source_bytes) - assert copy_image.tobytes() == source_bytes + im = hopper(mode) + source_bytes = im.tobytes() + + reloaded = Image.frombytes(mode, im.size, source_bytes) + assert reloaded.tobytes() == source_bytes @pytest.mark.parametrize("mode", image_mode_names) def test_roundtrip_bytes_method(self, mode: str): - source_image = hopper(mode) - source_bytes = source_image.tobytes() - copy_image = Image.new(mode, source_image.size) - copy_image.frombytes(source_bytes) - assert copy_image.tobytes() == source_bytes + im = hopper(mode) + source_bytes = im.tobytes() + + reloaded = Image.new(mode, im.size) + reloaded.frombytes(source_bytes) + assert reloaded.tobytes() == source_bytes @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes) def test_getdata_putdata( self, mode: str, num_bands: int, pixelsize: int ): - image_byte_size = 2 * 2 * pixelsize - start_bytes = bytes(range(image_byte_size)) - image = Image.frombytes(mode, (2, 2), start_bytes) - - start_pixels = ( - image.getpixel((0, 0)), - image.getpixel((0, 1)), - image.getpixel((1, 0)), - image.getpixel((1, 1)), - ) - - image.putdata(image.getdata()) - - end_pixels = ( - image.getpixel((0, 0)), - image.getpixel((0, 1)), - image.getpixel((1, 0)), - image.getpixel((1, 1)), - ) + start_bytes = bytes(range(2 * 2 * pixelsize)) + im = Image.frombytes(mode, (2, 2), start_bytes) - assert start_pixels == end_pixels + reloaded = Image.new(mode, im.size) + reloaded.putdata(im.getdata()) + assert_image_equal(im, reloaded) class MockEncoder(ImageFile.PyEncoder): From 10ceae924c72e278d5d0393055e360c25f9a081d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 18:52:55 +1100 Subject: [PATCH 3/5] Removed unused number of bands --- Tests/test_image.py | 56 ++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index d1817d9bfaf..44ff4527a80 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -33,34 +33,34 @@ skip_unless_feature, ) -# name, number of bands, pixel size +# name, pixel size image_modes = ( - ("1", 1, 1), - ("L", 1, 1), - ("LA", 2, 4), - ("La", 2, 4), - ("P", 1, 1), - ("PA", 2, 4), - ("F", 1, 4), - ("I", 1, 4), - ("I;16", 1, 2), - ("I;16L", 1, 2), - ("I;16B", 1, 2), - ("I;16N", 1, 2), - ("RGB", 3, 4), - ("RGBA", 4, 4), - ("RGBa", 4, 4), - ("RGBX", 4, 4), - ("BGR;15", 3, 2), - ("BGR;16", 3, 2), - ("BGR;24", 3, 3), - ("CMYK", 4, 4), - ("YCbCr", 3, 4), - ("HSV", 3, 4), - ("LAB", 3, 4), + ("1", 1), + ("L", 1), + ("LA", 4), + ("La", 4), + ("P", 1), + ("PA", 4), + ("F", 4), + ("I", 4), + ("I;16", 2), + ("I;16L", 2), + ("I;16B", 2), + ("I;16N", 2), + ("RGB", 4), + ("RGBA", 4), + ("RGBa", 4), + ("RGBX", 4), + ("BGR;15", 2), + ("BGR;16", 2), + ("BGR;24", 3), + ("CMYK", 4), + ("YCbCr", 4), + ("HSV", 4), + ("LAB", 4), ) -image_mode_names = [name for name, _, _ in image_modes] +image_mode_names = [name for name, _ in image_modes] class TestImage: @@ -1062,10 +1062,8 @@ def test_roundtrip_bytes_method(self, mode: str): reloaded.frombytes(source_bytes) assert reloaded.tobytes() == source_bytes - @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes) - def test_getdata_putdata( - self, mode: str, num_bands: int, pixelsize: int - ): + @pytest.mark.parametrize(("mode", "pixelsize"), image_modes) + def test_getdata_putdata(self, mode: str, pixelsize: int): start_bytes = bytes(range(2 * 2 * pixelsize)) im = Image.frombytes(mode, (2, 2), start_bytes) From a0ab9f488febc1543b8000ee1395dd2cf6fdcd50 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 19:12:48 +1100 Subject: [PATCH 4/5] Added type hints --- Tests/test_image.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 44ff4527a80..ab5ff640e8c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1046,7 +1046,7 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: class TestImageBytes: @pytest.mark.parametrize("mode", image_mode_names) - def test_roundtrip_bytes_constructor(self, mode: str): + def test_roundtrip_bytes_constructor(self, mode: str) -> None: im = hopper(mode) source_bytes = im.tobytes() @@ -1054,7 +1054,7 @@ def test_roundtrip_bytes_constructor(self, mode: str): assert reloaded.tobytes() == source_bytes @pytest.mark.parametrize("mode", image_mode_names) - def test_roundtrip_bytes_method(self, mode: str): + def test_roundtrip_bytes_method(self, mode: str) -> None: im = hopper(mode) source_bytes = im.tobytes() @@ -1063,7 +1063,7 @@ def test_roundtrip_bytes_method(self, mode: str): assert reloaded.tobytes() == source_bytes @pytest.mark.parametrize(("mode", "pixelsize"), image_modes) - def test_getdata_putdata(self, mode: str, pixelsize: int): + def test_getdata_putdata(self, mode: str, pixelsize: int) -> None: start_bytes = bytes(range(2 * 2 * pixelsize)) im = Image.frombytes(mode, (2, 2), start_bytes) From 9c41bf4641effc9d1a3aa229489449cb761f8958 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 19:16:06 +1100 Subject: [PATCH 5/5] Only specify image size once --- Tests/test_image.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index ab5ff640e8c..941ec40d9bc 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1064,8 +1064,9 @@ def test_roundtrip_bytes_method(self, mode: str) -> None: @pytest.mark.parametrize(("mode", "pixelsize"), image_modes) def test_getdata_putdata(self, mode: str, pixelsize: int) -> None: - start_bytes = bytes(range(2 * 2 * pixelsize)) - im = Image.frombytes(mode, (2, 2), start_bytes) + im = Image.new(mode, (2, 2)) + source_bytes = bytes(range(im.width * im.height * pixelsize)) + im.frombytes(source_bytes) reloaded = Image.new(mode, im.size) reloaded.putdata(im.getdata())