From 07f6951693ca32a72552909b588567353457e8b7 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Thu, 15 Dec 2022 23:09:26 -0600 Subject: [PATCH] add roundtrip tests for image from/to bytes --- Tests/test_image.py | 65 ++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index b4e81e466..69ba81564 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -28,32 +28,32 @@ from .helper import ( ) +all_modes = ( + "1", + "P", + "PA", + "L", + "LA", + "La", + "F", + "I", + "I;16", + "I;16L", + "I;16B", + "I;16N", + "RGB", + "RGBX", + "RGBA", + "RGBa", + "CMYK", + "YCbCr", + "LAB", + "HSV", +) + + 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", - "CMYK", - "YCbCr", - "LAB", - "HSV", - ), - ) + @pytest.mark.parametrize("mode", all_modes) def test_image_modes_success(self, mode): Image.new(mode, (1, 1)) @@ -862,6 +862,21 @@ class TestImage: im = Image.new("RGB", size) assert im.tobytes() == b"" + @pytest.mark.parametrize("mode", all_modes) + def test_roundtrip_bytes_constructor(self, mode): + source_image = hopper(mode) + source_bytes = 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 = image.tobytes() + copy_image = Image.new(mode, source_image.size) + copy_image.frombytes(source_bytes) + assert copy_image.tobytes() == source_bytes + def test_apply_transparency(self): im = Image.new("P", (1, 1)) im.putpalette((0, 0, 0, 1, 1, 1))