File: test_format_hsv.py

package info (click to toggle)
pillow 12.1.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 72,624 kB
  • sloc: python: 49,768; ansic: 38,750; makefile: 302; sh: 169; javascript: 85
file content (155 lines) | stat: -rw-r--r-- 3,790 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
150
151
152
153
154
155
from __future__ import annotations

import colorsys
import itertools

from PIL import Image

from .helper import assert_image_similar, hopper

TYPE_CHECKING = False
if TYPE_CHECKING:
    from collections.abc import Callable


def int_to_float(i: int) -> float:
    return i / 255


def tuple_to_ints(tp: tuple[float, float, float]) -> tuple[int, int, int]:
    x, y, z = tp
    return int(x * 255.0), int(y * 255.0), int(z * 255.0)


def test_sanity() -> None:
    Image.new("HSV", (100, 100))


def linear_gradient() -> Image.Image:
    im = Image.linear_gradient(mode="L")
    im90 = im.rotate(90)

    (px, h) = im.size

    r = Image.new("L", (px * 3, h))
    g = r.copy()
    b = r.copy()

    r.paste(im, (0, 0))
    r.paste(im90, (px, 0))

    g.paste(im90, (0, 0))
    g.paste(im, (2 * px, 0))

    b.paste(im, (px, 0))
    b.paste(im90, (2 * px, 0))

    return Image.merge("RGB", (r, g, b))


def to_xxx_colorsys(
    im: Image.Image,
    func: Callable[[float, float, float], tuple[float, float, float]],
    mode: str,
) -> Image.Image:
    # convert the hard way using the library colorsys routines.

    (r, g, b) = im.split()

    conv_func = int_to_float

    converted = [
        tuple_to_ints(func(conv_func(_r), conv_func(_g), conv_func(_b)))
        for (_r, _g, _b) in itertools.zip_longest(r.tobytes(), g.tobytes(), b.tobytes())
    ]

    new_bytes = b"".join(
        bytes(chr(h) + chr(s) + chr(v), "latin-1") for (h, s, v) in converted
    )

    hsv = Image.frombytes(mode, r.size, new_bytes)

    return hsv


def to_hsv_colorsys(im: Image.Image) -> Image.Image:
    return to_xxx_colorsys(im, colorsys.rgb_to_hsv, "HSV")


def to_rgb_colorsys(im: Image.Image) -> Image.Image:
    return to_xxx_colorsys(im, colorsys.hsv_to_rgb, "RGB")


def test_linear_gradient() -> None:
    src = linear_gradient().resize((3 * 32, 32), Image.Resampling.BILINEAR)
    im = src.convert("HSV")
    comparable = to_hsv_colorsys(src)

    assert_image_similar(
        im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong"
    )
    assert_image_similar(
        im.getchannel(1),
        comparable.getchannel(1),
        1,
        "Saturation conversion is wrong",
    )
    assert_image_similar(
        im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong"
    )

    comparable = src
    im = im.convert("RGB")

    assert_image_similar(
        im.getchannel(0), comparable.getchannel(0), 3, "R conversion is wrong"
    )
    assert_image_similar(
        im.getchannel(1), comparable.getchannel(1), 3, "G conversion is wrong"
    )
    assert_image_similar(
        im.getchannel(2), comparable.getchannel(2), 3, "B conversion is wrong"
    )


def test_convert() -> None:
    im = hopper("RGB").convert("HSV")
    comparable = to_hsv_colorsys(hopper("RGB"))

    assert_image_similar(
        im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong"
    )
    assert_image_similar(
        im.getchannel(1),
        comparable.getchannel(1),
        1,
        "Saturation conversion is wrong",
    )
    assert_image_similar(
        im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong"
    )


def test_hsv_to_rgb() -> None:
    comparable = to_hsv_colorsys(hopper("RGB"))
    converted = comparable.convert("RGB")
    comparable = to_rgb_colorsys(comparable)

    assert_image_similar(
        converted.getchannel(0),
        comparable.getchannel(0),
        3,
        "R conversion is wrong",
    )
    assert_image_similar(
        converted.getchannel(1),
        comparable.getchannel(1),
        3,
        "G conversion is wrong",
    )
    assert_image_similar(
        converted.getchannel(2),
        comparable.getchannel(2),
        3,
        "B conversion is wrong",
    )