add roundtrip tests for image from/to bytes

This commit is contained in:
Yay295 2022-12-15 23:09:26 -06:00 committed by GitHub
parent f4b1244213
commit 07f6951693
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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))