From a5e046fb4964f62837c229b9487fefe5758d1e54 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sun, 8 Jan 2023 14:37:46 +0200 Subject: [PATCH 1/2] Convert test_properties to use parametrize --- Tests/test_image_mode.py | 44 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/Tests/test_image_mode.py b/Tests/test_image_mode.py index 670b2f4ebde..6de2566b238 100644 --- a/Tests/test_image_mode.py +++ b/Tests/test_image_mode.py @@ -1,3 +1,5 @@ +import pytest + from PIL import Image, ImageMode from .helper import hopper @@ -49,23 +51,25 @@ def test_sanity(): assert m.typestr == "|u1" -def test_properties(): - def check(mode, *result): - signature = ( - Image.getmodebase(mode), - Image.getmodetype(mode), - Image.getmodebands(mode), - Image.getmodebandnames(mode), - ) - assert signature == result - - check("1", "L", "L", 1, ("1",)) - check("L", "L", "L", 1, ("L",)) - check("P", "P", "L", 1, ("P",)) - check("I", "L", "I", 1, ("I",)) - check("F", "L", "F", 1, ("F",)) - check("RGB", "RGB", "L", 3, ("R", "G", "B")) - check("RGBA", "RGB", "L", 4, ("R", "G", "B", "A")) - check("RGBX", "RGB", "L", 4, ("R", "G", "B", "X")) - check("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K")) - check("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr")) +@pytest.mark.parametrize( + "mode, expected_base, expected_type, expected_bands, expected_band_names", + ( + ("1", "L", "L", 1, ("1",)), + ("L", "L", "L", 1, ("L",)), + ("P", "P", "L", 1, ("P",)), + ("I", "L", "I", 1, ("I",)), + ("F", "L", "F", 1, ("F",)), + ("RGB", "RGB", "L", 3, ("R", "G", "B")), + ("RGBA", "RGB", "L", 4, ("R", "G", "B", "A")), + ("RGBX", "RGB", "L", 4, ("R", "G", "B", "X")), + ("CMYK", "RGB", "L", 4, ("C", "M", "Y", "K")), + ("YCbCr", "RGB", "L", 3, ("Y", "Cb", "Cr")), + ), +) +def test_properties( + mode, expected_base, expected_type, expected_bands, expected_band_names +): + assert Image.getmodebase(mode) == expected_base + assert Image.getmodetype(mode) == expected_type + assert Image.getmodebands(mode) == expected_bands + assert Image.getmodebandnames(mode) == expected_band_names From e24dd745f7386f52d7a617a9cb5c61fbd1d0ade0 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade Date: Sun, 8 Jan 2023 14:48:56 +0200 Subject: [PATCH 2/2] Convert test_optimize_correctness to use parametrize --- Tests/test_file_gif.py | 67 ++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index d48fc144205..6fbc0ee3009 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -158,39 +158,42 @@ def test_bilevel(optimize): assert test_bilevel(1) == 799 -def test_optimize_correctness(): - # 256 color Palette image, posterize to > 128 and < 128 levels - # Size bigger and smaller than 512x512 +@pytest.mark.parametrize( + "colors, size, expected_palette_length", + ( + # These do optimize the palette + (256, 511, 256), + (255, 511, 255), + (129, 511, 129), + (128, 511, 128), + (64, 511, 64), + (4, 511, 4), + # These don't optimize the palette + (128, 513, 256), + (64, 513, 256), + (4, 513, 256), + ), +) +def test_optimize_correctness(colors, size, expected_palette_length): + # 256 color Palette image, posterize to > 128 and < 128 levels. + # Size bigger and smaller than 512x512. # Check the palette for number of colors allocated. - # Check for correctness after conversion back to RGB - def check(colors, size, expected_palette_length): - # make an image with empty colors in the start of the palette range - im = Image.frombytes( - "P", (colors, colors), bytes(range(256 - colors, 256)) * colors - ) - im = im.resize((size, size)) - outfile = BytesIO() - im.save(outfile, "GIF") - outfile.seek(0) - with Image.open(outfile) as reloaded: - # check palette length - palette_length = max(i + 1 for i, v in enumerate(reloaded.histogram()) if v) - assert expected_palette_length == palette_length - - assert_image_equal(im.convert("RGB"), reloaded.convert("RGB")) - - # These do optimize the palette - check(256, 511, 256) - check(255, 511, 255) - check(129, 511, 129) - check(128, 511, 128) - check(64, 511, 64) - check(4, 511, 4) - - # These don't optimize the palette - check(128, 513, 256) - check(64, 513, 256) - check(4, 513, 256) + # Check for correctness after conversion back to RGB. + + # make an image with empty colors in the start of the palette range + im = Image.frombytes( + "P", (colors, colors), bytes(range(256 - colors, 256)) * colors + ) + im = im.resize((size, size)) + outfile = BytesIO() + im.save(outfile, "GIF") + outfile.seek(0) + with Image.open(outfile) as reloaded: + # check palette length + palette_length = max(i + 1 for i, v in enumerate(reloaded.histogram()) if v) + assert expected_palette_length == palette_length + + assert_image_equal(im.convert("RGB"), reloaded.convert("RGB")) def test_optimize_full_l():