Pillow/Tests/test_format_hsv.py

145 lines
3.9 KiB
Python
Raw Normal View History

import colorsys
import itertools
2014-07-22 09:36:40 +04:00
from PIL import Image
from .helper import PillowTestCase, assert_image_similar, hopper
2014-08-28 15:44:19 +04:00
2014-07-22 09:36:40 +04:00
class TestFormatHSV(PillowTestCase):
def int_to_float(self, i):
return i / 255
2014-08-28 15:44:19 +04:00
2014-07-22 09:36:40 +04:00
def str_to_float(self, i):
return ord(i) / 255
2014-08-28 15:44:19 +04:00
2014-07-23 19:49:19 +04:00
def tuple_to_ints(self, tp):
2014-08-28 15:44:19 +04:00
x, y, z = tp
2019-06-13 18:54:24 +03:00
return int(x * 255.0), int(y * 255.0), int(z * 255.0)
2014-08-28 15:44:19 +04:00
2014-07-22 09:36:40 +04:00
def test_sanity(self):
2019-06-13 18:54:24 +03:00
Image.new("HSV", (100, 100))
2014-07-22 09:36:40 +04:00
def wedge(self):
2014-08-28 15:44:19 +04:00
w = Image._wedge()
2014-07-22 09:36:40 +04:00
w90 = w.rotate(90)
(px, h) = w.size
2014-08-28 15:44:19 +04:00
2019-06-13 18:54:24 +03:00
r = Image.new("L", (px * 3, h))
2014-07-22 09:36:40 +04:00
g = r.copy()
b = r.copy()
2014-08-28 15:44:19 +04:00
r.paste(w, (0, 0))
r.paste(w90, (px, 0))
2014-07-22 09:36:40 +04:00
2014-08-28 15:44:19 +04:00
g.paste(w90, (0, 0))
2019-06-13 18:54:24 +03:00
g.paste(w, (2 * px, 0))
2014-07-22 09:36:40 +04:00
2014-08-28 15:44:19 +04:00
b.paste(w, (px, 0))
2019-06-13 18:54:24 +03:00
b.paste(w90, (2 * px, 0))
2014-07-22 09:36:40 +04:00
2019-06-13 18:54:24 +03:00
img = Image.merge("RGB", (r, g, b))
2014-07-22 09:36:40 +04:00
return img
2014-08-28 15:44:19 +04:00
2014-07-22 09:36:40 +04:00
def to_xxx_colorsys(self, im, func, mode):
# convert the hard way using the library colorsys routines.
2014-08-28 15:44:19 +04:00
(r, g, b) = im.split()
2019-09-26 15:12:28 +03:00
conv_func = self.int_to_float
2014-07-22 09:36:40 +04:00
2019-06-13 18:54:24 +03:00
converted = [
self.tuple_to_ints(func(conv_func(_r), conv_func(_g), conv_func(_b)))
2019-09-26 15:12:28 +03:00
for (_r, _g, _b) in itertools.zip_longest(
r.tobytes(), g.tobytes(), b.tobytes()
)
2019-06-13 18:54:24 +03:00
]
2014-07-23 19:49:19 +04:00
2019-09-26 15:12:28 +03:00
new_bytes = b"".join(
bytes(chr(h) + chr(s) + chr(v), "latin-1") for (h, s, v) in converted
)
2014-08-28 15:44:19 +04:00
hsv = Image.frombytes(mode, r.size, new_bytes)
2014-07-22 09:36:40 +04:00
return hsv
def to_hsv_colorsys(self, im):
2019-06-13 18:54:24 +03:00
return self.to_xxx_colorsys(im, colorsys.rgb_to_hsv, "HSV")
2014-07-22 09:36:40 +04:00
def to_rgb_colorsys(self, im):
2019-06-13 18:54:24 +03:00
return self.to_xxx_colorsys(im, colorsys.hsv_to_rgb, "RGB")
2014-07-22 09:36:40 +04:00
def test_wedge(self):
2019-06-13 18:54:24 +03:00
src = self.wedge().resize((3 * 32, 32), Image.BILINEAR)
im = src.convert("HSV")
2014-07-23 19:49:19 +04:00
comparable = self.to_hsv_colorsys(src)
2014-07-22 09:36:40 +04:00
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong"
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(1),
comparable.getchannel(1),
1,
"Saturation conversion is wrong",
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong"
)
2014-07-22 09:36:40 +04:00
2014-07-23 19:49:19 +04:00
comparable = src
2019-06-13 18:54:24 +03:00
im = im.convert("RGB")
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(0), comparable.getchannel(0), 3, "R conversion is wrong"
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(1), comparable.getchannel(1), 3, "G conversion is wrong"
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(2), comparable.getchannel(2), 3, "B conversion is wrong"
)
2014-08-28 15:44:19 +04:00
2014-07-22 09:36:40 +04:00
def test_convert(self):
2019-06-13 18:54:24 +03:00
im = hopper("RGB").convert("HSV")
comparable = self.to_hsv_colorsys(hopper("RGB"))
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(0), comparable.getchannel(0), 1, "Hue conversion is wrong"
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(1),
comparable.getchannel(1),
1,
"Saturation conversion is wrong",
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
im.getchannel(2), comparable.getchannel(2), 1, "Value conversion is wrong"
)
2014-07-22 09:36:40 +04:00
def test_hsv_to_rgb(self):
2019-06-13 18:54:24 +03:00
comparable = self.to_hsv_colorsys(hopper("RGB"))
converted = comparable.convert("RGB")
2014-07-22 09:36:40 +04:00
comparable = self.to_rgb_colorsys(comparable)
assert_image_similar(
2019-06-13 18:54:24 +03:00
converted.getchannel(0),
comparable.getchannel(0),
3,
"R conversion is wrong",
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
converted.getchannel(1),
comparable.getchannel(1),
3,
"G conversion is wrong",
)
assert_image_similar(
2019-06-13 18:54:24 +03:00
converted.getchannel(2),
comparable.getchannel(2),
3,
"B conversion is wrong",
)