Pillow/Tests/test_image_split.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2024-01-25 14:18:46 +03:00
from pathlib import Path
2022-10-03 08:57:42 +03:00
import pytest
from PIL import Image, features
2020-02-25 12:57:27 +03:00
from .helper import assert_image_equal, hopper
2024-01-25 14:18:46 +03:00
def test_split() -> None:
def split(mode: str) -> list[tuple[str, int, int]]:
2020-02-25 12:57:27 +03:00
layers = hopper(mode).split()
return [(i.mode, i.size[0], i.size[1]) for i in layers]
assert split("1") == [("1", 128, 128)]
assert split("L") == [("L", 128, 128)]
assert split("I") == [("I", 128, 128)]
assert split("F") == [("F", 128, 128)]
assert split("P") == [("P", 128, 128)]
assert split("RGB") == [("L", 128, 128), ("L", 128, 128), ("L", 128, 128)]
assert split("RGBA") == [
("L", 128, 128),
("L", 128, 128),
("L", 128, 128),
("L", 128, 128),
]
assert split("CMYK") == [
("L", 128, 128),
("L", 128, 128),
("L", 128, 128),
("L", 128, 128),
]
assert split("YCbCr") == [("L", 128, 128), ("L", 128, 128), ("L", 128, 128)]
2022-10-03 08:57:42 +03:00
@pytest.mark.parametrize(
"mode", ("1", "L", "I", "F", "P", "RGB", "RGBA", "CMYK", "YCbCr")
)
2024-01-25 14:18:46 +03:00
def test_split_merge(mode: str) -> None:
2022-10-03 08:57:42 +03:00
expected = Image.merge(mode, hopper(mode).split())
assert_image_equal(hopper(mode), expected)
2020-02-25 12:57:27 +03:00
2024-01-25 14:18:46 +03:00
def test_split_open(tmp_path: Path) -> None:
2020-02-25 12:57:27 +03:00
if features.check("zlib"):
test_file = str(tmp_path / "temp.png")
else:
test_file = str(tmp_path / "temp.pcx")
2024-01-25 14:18:46 +03:00
def split_open(mode: str) -> int:
2020-02-25 12:57:27 +03:00
hopper(mode).save(test_file)
with Image.open(test_file) as im:
return len(im.split())
assert split_open("1") == 1
assert split_open("L") == 1
assert split_open("P") == 1
assert split_open("RGB") == 3
if features.check("zlib"):
assert split_open("RGBA") == 4