File: test_image_transpose.py

package info (click to toggle)
pillow 12.1.1-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 72,640 kB
  • sloc: python: 49,768; ansic: 38,750; makefile: 302; sh: 169; javascript: 85
file content (149 lines) | stat: -rw-r--r-- 5,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from __future__ import annotations

import pytest

from PIL import Image
from PIL.Image import Transpose

from . import helper
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"]
}


@pytest.mark.parametrize("mode", HOPPER)
def test_flip_left_right(mode: str) -> None:
    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)
def test_flip_top_bottom(mode: str) -> None:
    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)
def test_rotate_90(mode: str) -> None:
    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)
def test_rotate_180(mode: str) -> None:
    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)
def test_rotate_270(mode: str) -> None:
    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)
def test_transpose(mode: str) -> None:
    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)
def test_tranverse(mode: str) -> None:
    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)
def test_roundtrip(mode: str) -> None:
    im = HOPPER[mode]

    def transpose(first: Transpose, second: Transpose) -> Image.Image:
        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),
    )