Pillow/Tests/test_image_transpose.py

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

150 lines
4.9 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2022-08-23 14:41:32 +03:00
import pytest
2024-01-25 14:18:46 +03:00
from PIL import Image
2022-01-15 01:02:31 +03:00
from PIL.Image import Transpose
from . import helper
2020-02-12 19:29:19 +03:00
from .helper import assert_image_equal
HOPPER = {
mode: helper.hopper(mode).crop((0, 0, 121, 127)).copy()
for mode in ["L", "RGB", "I;16", "I;16L", "I;16B"]
}
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_flip_left_right(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.FLIP_LEFT_RIGHT)
assert out.mode == mode
assert out.size == im.size
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((x - 2, 1))
assert im.getpixel((x - 2, 1)) == out.getpixel((1, 1))
assert im.getpixel((1, y - 2)) == out.getpixel((x - 2, y - 2))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((1, y - 2))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_flip_top_bottom(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.FLIP_TOP_BOTTOM)
assert out.mode == mode
assert out.size == im.size
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((1, y - 2))
assert im.getpixel((x - 2, 1)) == out.getpixel((x - 2, y - 2))
assert im.getpixel((1, y - 2)) == out.getpixel((1, 1))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((x - 2, 1))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_rotate_90(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.ROTATE_90)
assert out.mode == mode
assert out.size == im.size[::-1]
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((1, x - 2))
assert im.getpixel((x - 2, 1)) == out.getpixel((1, 1))
assert im.getpixel((1, y - 2)) == out.getpixel((y - 2, x - 2))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((y - 2, 1))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_rotate_180(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.ROTATE_180)
assert out.mode == mode
assert out.size == im.size
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((x - 2, y - 2))
assert im.getpixel((x - 2, 1)) == out.getpixel((1, y - 2))
assert im.getpixel((1, y - 2)) == out.getpixel((x - 2, 1))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((1, 1))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_rotate_270(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.ROTATE_270)
assert out.mode == mode
assert out.size == im.size[::-1]
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((y - 2, 1))
assert im.getpixel((x - 2, 1)) == out.getpixel((y - 2, x - 2))
assert im.getpixel((1, y - 2)) == out.getpixel((1, 1))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((1, x - 2))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_transpose(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.TRANSPOSE)
assert out.mode == mode
assert out.size == im.size[::-1]
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((1, 1))
assert im.getpixel((x - 2, 1)) == out.getpixel((1, x - 2))
assert im.getpixel((1, y - 2)) == out.getpixel((y - 2, 1))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((y - 2, x - 2))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_tranverse(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
out = im.transpose(Transpose.TRANSVERSE)
assert out.mode == mode
assert out.size == im.size[::-1]
x, y = im.size
assert im.getpixel((1, 1)) == out.getpixel((y - 2, x - 2))
assert im.getpixel((x - 2, 1)) == out.getpixel((y - 2, 1))
assert im.getpixel((1, y - 2)) == out.getpixel((1, x - 2))
assert im.getpixel((x - 2, y - 2)) == out.getpixel((1, 1))
@pytest.mark.parametrize("mode", HOPPER)
2024-01-25 14:18:46 +03:00
def test_roundtrip(mode: str) -> None:
2022-08-23 14:41:32 +03:00
im = HOPPER[mode]
2024-01-25 14:18:46 +03:00
def transpose(first: Transpose, second: Transpose) -> Image.Image:
2022-08-23 14:41:32 +03:00
return im.transpose(first).transpose(second)
assert_image_equal(
im, transpose(Transpose.FLIP_LEFT_RIGHT, Transpose.FLIP_LEFT_RIGHT)
)
assert_image_equal(
im, transpose(Transpose.FLIP_TOP_BOTTOM, Transpose.FLIP_TOP_BOTTOM)
)
assert_image_equal(im, transpose(Transpose.ROTATE_90, Transpose.ROTATE_270))
assert_image_equal(im, transpose(Transpose.ROTATE_180, Transpose.ROTATE_180))
assert_image_equal(
im.transpose(Transpose.TRANSPOSE),
transpose(Transpose.ROTATE_90, Transpose.FLIP_TOP_BOTTOM),
)
assert_image_equal(
im.transpose(Transpose.TRANSPOSE),
transpose(Transpose.ROTATE_270, Transpose.FLIP_LEFT_RIGHT),
)
assert_image_equal(
im.transpose(Transpose.TRANSVERSE),
transpose(Transpose.ROTATE_90, Transpose.FLIP_LEFT_RIGHT),
)
assert_image_equal(
im.transpose(Transpose.TRANSVERSE),
transpose(Transpose.ROTATE_270, Transpose.FLIP_TOP_BOTTOM),
)
assert_image_equal(
im.transpose(Transpose.TRANSVERSE),
transpose(Transpose.ROTATE_180, Transpose.TRANSPOSE),
)