From 87ed513f91d132e6eb48a02409f212ef0c2ae9a1 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Sun, 25 Dec 2022 18:41:21 -0600 Subject: [PATCH 01/14] support "RGBa" and "La" in hopper() test helper --- Tests/helper.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/helper.py b/Tests/helper.py index 9849bf65565..a7691f64527 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -261,7 +261,11 @@ def hopper(mode: str | None = None, cache: dict[str, Image.Image] = {}) -> Image # (for fast, isolated, repeatable tests). im = cache.get(mode) if im is None: - if mode == "F": + if mode == "RGBa": + im = hopper("RGBA").convert(mode) + elif mode == "La": + im = hopper("LA").convert(mode) + elif mode == "F": im = hopper("L").convert(mode) elif mode[:4] == "I;16": im = hopper("I").convert(mode) From a626ff0e607ab6ed04b990a2b4868d5c35c18ed1 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Sun, 25 Dec 2022 18:43:39 -0600 Subject: [PATCH 02/14] add tests using the bytes Image class methods --- Tests/test_image.py | 84 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/Tests/test_image.py b/Tests/test_image.py index d472bd7ea48..3baf9581013 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1042,6 +1042,90 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: assert im.fp is None +class TestImageBytes: + # modes grouped by pixel size + one_byte_modes = ( + "1", + "L", + "P", + ) + two_byte_modes = ( + "I;16", + "I;16L", + "I;16B", + # "I;16N", # unknown raw mode for given image mode + # "BGR;15", # unrecognized image mode + # "BGR;16", # unrecognized image mode + ) + # three_byte_modes = ("BGR;24",) # unrecognized image mode + three_byte_modes = () + four_byte_modes = ( + "LA", + "La", + "PA", + "F", + "I", + # "BGR;32", # unrecognized image mode + "RGB", + "RGBA", + "RGBa", + "RGBX", + "CMYK", + "YCbCr", + "HSV", + "LAB", + ) + all_modes = one_byte_modes + two_byte_modes + three_byte_modes + four_byte_modes + + # make this bigger if necessary + sample_bytes = bytes(range(16)) + + @pytest.mark.parametrize("mode", all_modes) + def test_roundtrip_bytes_constructor(self, mode): + 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 + + @pytest.mark.parametrize("mode", all_modes) + def test_roundtrip_bytes_method(self, mode): + 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 + + @pytest.mark.parametrize( + ("mode", "pixelsize"), + tuple((m, 1) for m in one_byte_modes) + + tuple((m, 2) for m in two_byte_modes) + + tuple((m, 3) for m in three_byte_modes) + + tuple((m, 4) for m in four_byte_modes), + ) + def test_get_pixel(self, mode, pixelsize): + image_byte_size = 2 * 2 * pixelsize + start_bytes = self.sample_bytes[: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)), + ) + + assert start_pixels == end_pixels + + class MockEncoder(ImageFile.PyEncoder): pass From 43f3c822a7b157ca31fa7be84f08194694f9fd98 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Tue, 27 Dec 2022 21:11:05 -0600 Subject: [PATCH 03/14] use tuple of tuples for image mode info --- Tests/test_image.py | 110 ++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 71 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 3baf9581013..f7fd664c034 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -33,36 +33,38 @@ skip_unless_feature, ) +# name, number of bands, 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), +) + +image_mode_names = [name for name, num_bands, pixelsize in image_modes] + class TestImage: - @pytest.mark.parametrize( - "mode", - ( - "1", - "P", - "PA", - "L", - "LA", - "La", - "F", - "I", - "I;16", - "I;16L", - "I;16B", - "I;16N", - "RGB", - "RGBX", - "RGBA", - "RGBa", - "BGR;15", - "BGR;16", - "BGR;24", - "CMYK", - "YCbCr", - "LAB", - "HSV", - ), - ) + @pytest.mark.parametrize("mode", image_mode_names) def test_image_modes_success(self, mode: str) -> None: Image.new(mode, (1, 1)) @@ -1043,51 +1045,23 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: class TestImageBytes: - # modes grouped by pixel size - one_byte_modes = ( - "1", - "L", - "P", - ) - two_byte_modes = ( - "I;16", - "I;16L", - "I;16B", - # "I;16N", # unknown raw mode for given image mode - # "BGR;15", # unrecognized image mode - # "BGR;16", # unrecognized image mode - ) - # three_byte_modes = ("BGR;24",) # unrecognized image mode - three_byte_modes = () - four_byte_modes = ( - "LA", - "La", - "PA", - "F", - "I", - # "BGR;32", # unrecognized image mode - "RGB", - "RGBA", - "RGBa", - "RGBX", - "CMYK", - "YCbCr", - "HSV", - "LAB", - ) - all_modes = one_byte_modes + two_byte_modes + three_byte_modes + four_byte_modes + # The BGR modes don't support the methods tested in this class. + image_modes_not_bgr = [mode for mode in image_modes if "BGR" not in mode[0]] + image_mode_names_not_bgr = [ + name for name, num_bands, pixelsize in image_modes if "BGR" not in name + ] # make this bigger if necessary sample_bytes = bytes(range(16)) - @pytest.mark.parametrize("mode", all_modes) + @pytest.mark.parametrize("mode", image_mode_names_not_bgr) def test_roundtrip_bytes_constructor(self, mode): 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 - @pytest.mark.parametrize("mode", all_modes) + @pytest.mark.parametrize("mode", image_mode_names_not_bgr) def test_roundtrip_bytes_method(self, mode): source_image = hopper(mode) source_bytes = source_image.tobytes() @@ -1095,14 +1069,8 @@ def test_roundtrip_bytes_method(self, mode): copy_image.frombytes(source_bytes) assert copy_image.tobytes() == source_bytes - @pytest.mark.parametrize( - ("mode", "pixelsize"), - tuple((m, 1) for m in one_byte_modes) - + tuple((m, 2) for m in two_byte_modes) - + tuple((m, 3) for m in three_byte_modes) - + tuple((m, 4) for m in four_byte_modes), - ) - def test_get_pixel(self, mode, pixelsize): + @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes_not_bgr) + def test_pixels_after_getdata_putdata(self, mode, num_bands, pixelsize): image_byte_size = 2 * 2 * pixelsize start_bytes = self.sample_bytes[:image_byte_size] image = Image.frombytes(mode, (2, 2), start_bytes) From 79c9b2b26116a9671d0d814eab55284777830370 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Sat, 10 Jun 2023 21:23:46 -0500 Subject: [PATCH 04/14] autosize TestImageBytes.sample_bytes --- Tests/test_image.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index f7fd664c034..780c843e20d 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1051,8 +1051,11 @@ class TestImageBytes: name for name, num_bands, pixelsize in image_modes if "BGR" not in name ] - # make this bigger if necessary - sample_bytes = bytes(range(16)) + sample_bytes = bytes( + range( + 2 * 2 * max(pixelsize for mode, num_bands, pixelsize in image_modes_not_bgr) + ) + ) @pytest.mark.parametrize("mode", image_mode_names_not_bgr) def test_roundtrip_bytes_constructor(self, mode): From c56c8901b7800f09a06afdaf4d70eac92bdca176 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Mon, 26 Feb 2024 01:00:11 -0600 Subject: [PATCH 05/14] handle I;16 native endianness on big-endian machine --- src/_imaging.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/_imaging.c b/src/_imaging.c index 59f80a35415..520e5079346 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1578,7 +1578,17 @@ if (PySequence_Check(op)) { \ int bigendian = 0; if (image->type == IMAGING_TYPE_SPECIAL) { // I;16* - bigendian = strcmp(image->mode, "I;16B") == 0; + if (strcmp(image->mode, "I;16N") == 0) { +#ifdef WORDS_BIGENDIAN + bigendian = 1; +#else + bigendian = 0; +#endif + } else if (strcmp(image->mode, "I;16B") == 0) { + bigendian = 1; + } else { + bigendian = 0; + } } for (i = x = y = 0; i < n; i++) { set_value_to_item(seq, i); From 12d626a6bbac15ed7193dd50222c3e7ea5b0b613 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Mon, 11 Mar 2024 12:00:13 -0500 Subject: [PATCH 06/14] add typing to image bytes tests --- Tests/test_image.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 780c843e20d..87a9788611c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1058,14 +1058,14 @@ class TestImageBytes: ) @pytest.mark.parametrize("mode", image_mode_names_not_bgr) - def test_roundtrip_bytes_constructor(self, mode): + 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 @pytest.mark.parametrize("mode", image_mode_names_not_bgr) - def test_roundtrip_bytes_method(self, mode): + 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) @@ -1073,7 +1073,9 @@ def test_roundtrip_bytes_method(self, mode): assert copy_image.tobytes() == source_bytes @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes_not_bgr) - def test_pixels_after_getdata_putdata(self, mode, num_bands, pixelsize): + def test_pixels_after_getdata_putdata( + self, mode: str, num_bands: int, pixelsize: int + ): image_byte_size = 2 * 2 * pixelsize start_bytes = self.sample_bytes[:image_byte_size] image = Image.frombytes(mode, (2, 2), start_bytes) From 680f387bc2d5d5d0171735e152638e936c2295f1 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Mon, 11 Mar 2024 12:00:48 -0500 Subject: [PATCH 07/14] enable bgr modes for image bytes tests --- Tests/test_image.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 87a9788611c..8890db1289f 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1045,26 +1045,18 @@ def test_close_graceful(self, caplog: pytest.LogCaptureFixture) -> None: class TestImageBytes: - # The BGR modes don't support the methods tested in this class. - image_modes_not_bgr = [mode for mode in image_modes if "BGR" not in mode[0]] - image_mode_names_not_bgr = [ - name for name, num_bands, pixelsize in image_modes if "BGR" not in name - ] - sample_bytes = bytes( - range( - 2 * 2 * max(pixelsize for mode, num_bands, pixelsize in image_modes_not_bgr) - ) + range(2 * 2 * max(pixelsize for mode, num_bands, pixelsize in image_modes)) ) - @pytest.mark.parametrize("mode", image_mode_names_not_bgr) + @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 - @pytest.mark.parametrize("mode", image_mode_names_not_bgr) + @pytest.mark.parametrize("mode", image_mode_names) def test_roundtrip_bytes_method(self, mode: str): source_image = hopper(mode) source_bytes = source_image.tobytes() @@ -1072,7 +1064,7 @@ def test_roundtrip_bytes_method(self, mode: str): copy_image.frombytes(source_bytes) assert copy_image.tobytes() == source_bytes - @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes_not_bgr) + @pytest.mark.parametrize(("mode", "num_bands", "pixelsize"), image_modes) def test_pixels_after_getdata_putdata( self, mode: str, num_bands: int, pixelsize: int ): From d30404feded2eb9d16503fa641267b21639b05a3 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Wed, 20 Mar 2024 08:55:05 -0500 Subject: [PATCH 08/14] use dummy var name for unused parts of unpacked tuple Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Tests/test_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 8890db1289f..1c2b6d4b938 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -60,7 +60,7 @@ ("LAB", 3, 4), ) -image_mode_names = [name for name, num_bands, pixelsize in image_modes] +image_mode_names = [name for name, _, _ in image_modes] class TestImage: From 75a206bf5886dfa7f1f322bcc743eac51c3a566a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 20 Mar 2024 20:42:13 +1100 Subject: [PATCH 09/14] Added conversion from RGB to RGBa and La --- Tests/helper.py | 6 +----- src/libImaging/Convert.c | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Tests/helper.py b/Tests/helper.py index a7691f64527..9849bf65565 100644 --- a/Tests/helper.py +++ b/Tests/helper.py @@ -261,11 +261,7 @@ def hopper(mode: str | None = None, cache: dict[str, Image.Image] = {}) -> Image # (for fast, isolated, repeatable tests). im = cache.get(mode) if im is None: - if mode == "RGBa": - im = hopper("RGBA").convert(mode) - elif mode == "La": - im = hopper("LA").convert(mode) - elif mode == "F": + if mode == "F": im = hopper("L").convert(mode) elif mode[:4] == "I;16": im = hopper("I").convert(mode) diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 5cc39cd0019..de94ed159f0 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -941,12 +941,14 @@ static struct { {"RGB", "1", rgb2bit}, {"RGB", "L", rgb2l}, {"RGB", "LA", rgb2la}, + {"RGB", "La", rgb2la}, {"RGB", "I", rgb2i}, {"RGB", "F", rgb2f}, {"RGB", "BGR;15", rgb2bgr15}, {"RGB", "BGR;16", rgb2bgr16}, {"RGB", "BGR;24", rgb2bgr24}, {"RGB", "RGBA", rgb2rgba}, + {"RGB", "RGBa", rgb2rgba}, {"RGB", "RGBX", rgb2rgba}, {"RGB", "CMYK", rgb2cmyk}, {"RGB", "YCbCr", ImagingConvertRGB2YCbCr}, From fd80b2e1d970e7ff115ce9a13b4311a7127c0da1 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 21 Mar 2024 18:46:09 +1100 Subject: [PATCH 10/14] 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 11/14] 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 12/14] 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 13/14] 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 14/14] 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())