1
from __future__ import annotations
3
from pathlib import Path
9
from .helper import assert_image, assert_image_equal, assert_image_similar, hopper
12
def test_sanity() -> None:
13
def convert(im: Image.Image, mode: str) -> None:
14
out = im.convert(mode)
15
assert out.mode == mode
16
assert out.size == im.size
34
for input_mode in modes:
35
im = hopper(input_mode)
36
for output_mode in modes:
37
convert(im, output_mode)
40
im = Image.new(input_mode, (0, 0))
41
for output_mode in modes:
42
convert(im, output_mode)
45
def test_unsupported_conversion() -> None:
47
with pytest.raises(ValueError):
51
def test_default() -> None:
54
converted_im = im.convert()
55
assert_image(converted_im, "RGB", im.size)
56
converted_im = im.convert()
57
assert_image(converted_im, "RGB", im.size)
59
im.info["transparency"] = 0
60
converted_im = im.convert()
61
assert_image(converted_im, "RGBA", im.size)
67
def _test_float_conversion(im: Image.Image) -> None:
68
orig = im.getpixel((5, 5))
69
converted = im.convert("F").getpixel((5, 5))
70
assert orig == converted
73
def test_8bit() -> None:
74
with Image.open("Tests/images/hopper.jpg") as im:
75
_test_float_conversion(im.convert("L"))
78
def test_16bit() -> None:
79
with Image.open("Tests/images/16bit.cropped.tif") as im:
80
_test_float_conversion(im)
82
for color in (65535, 65536):
83
im = Image.new("I", (1, 1), color)
84
im_i16 = im.convert("I;16")
85
assert im_i16.getpixel((0, 0)) == 65535
88
def test_16bit_workaround() -> None:
89
with Image.open("Tests/images/16bit.cropped.tif") as im:
90
_test_float_conversion(im.convert("I"))
93
def test_opaque() -> None:
94
alpha = hopper("P").convert("PA").getchannel("A")
96
solid = Image.new("L", (128, 128), 255)
97
assert_image_equal(alpha, solid)
100
def test_rgba_p() -> None:
102
im.putalpha(hopper("L"))
104
converted = im.convert("P")
105
comparable = converted.convert("RGBA")
107
assert_image_similar(im, comparable, 20)
110
def test_rgba() -> None:
111
with Image.open("Tests/images/transparent.png") as im:
112
assert im.mode == "RGBA"
114
assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)
117
def test_trns_p(tmp_path: Path) -> None:
119
im.info["transparency"] = 0
121
f = str(tmp_path / "temp.png")
123
im_l = im.convert("L")
124
assert im_l.info["transparency"] == 0
127
im_rgb = im.convert("RGB")
128
assert im_rgb.info["transparency"] == (0, 0, 0)
135
@pytest.mark.parametrize("mode", ("LA", "PA", "RGBA"))
136
def test_trns_p_transparency(mode: str) -> None:
139
im.info["transparency"] = 128
142
converted_im = im.convert(mode)
145
assert "transparency" not in converted_im.info
147
assert converted_im.palette is not None
150
assert converted_im.palette is None
153
def test_trns_l(tmp_path: Path) -> None:
155
im.info["transparency"] = 128
157
f = str(tmp_path / "temp.png")
159
im_la = im.convert("LA")
160
assert "transparency" not in im_la.info
163
im_rgb = im.convert("RGB")
164
assert im_rgb.info["transparency"] == (128, 128, 128)
167
im_p = im.convert("P")
168
assert "transparency" in im_p.info
171
im_p = im.convert("P", palette=Image.Palette.ADAPTIVE)
172
assert "transparency" in im_p.info
176
def test_trns_RGB(tmp_path: Path) -> None:
178
im.info["transparency"] = im.getpixel((0, 0))
180
f = str(tmp_path / "temp.png")
182
im_l = im.convert("L")
183
assert im_l.info["transparency"] == im_l.getpixel((0, 0))
186
im_la = im.convert("LA")
187
assert "transparency" not in im_la.info
190
im_la = im.convert("La")
191
assert "transparency" not in im_la.info
192
assert im_la.getpixel((0, 0)) == (0, 0)
194
im_p = im.convert("P")
195
assert "transparency" in im_p.info
198
im_rgba = im.convert("RGBA")
199
assert "transparency" not in im_rgba.info
202
im_rgba = im.convert("RGBa")
203
assert "transparency" not in im_rgba.info
204
assert im_rgba.getpixel((0, 0)) == (0, 0, 0, 0)
206
im_p = pytest.warns(UserWarning, im.convert, "P", palette=Image.Palette.ADAPTIVE)
207
assert "transparency" not in im_p.info
210
im = Image.new("RGB", (1, 1))
211
im.info["transparency"] = im.getpixel((0, 0))
212
im_p = im.convert("P", palette=Image.Palette.ADAPTIVE)
213
assert im_p.info["transparency"] == im_p.getpixel((0, 0))
217
@pytest.mark.parametrize("convert_mode", ("L", "LA", "I"))
218
def test_l_macro_rounding(convert_mode: str) -> None:
219
for mode in ("P", "PA"):
220
im = Image.new(mode, (1, 1))
221
assert im.palette is not None
222
im.palette.getcolor((0, 1, 2))
224
converted_im = im.convert(convert_mode)
225
px = converted_im.load()
226
assert px is not None
227
converted_color = px[0, 0]
228
if convert_mode == "LA":
229
assert isinstance(converted_color, tuple)
230
converted_color = converted_color[0]
231
assert converted_color == 1
234
def test_gif_with_rgba_palette_to_p() -> None:
236
with Image.open("Tests/images/hopper.gif") as im:
237
im.info["transparency"] = 255
239
assert im.palette.mode == "RGB"
240
im_p = im.convert("P")
246
def test_p_la() -> None:
251
comparable = im.convert("P").convert("LA").getchannel("A")
253
assert_image_similar(alpha, comparable, 5)
256
def test_p2pa_alpha() -> None:
257
with Image.open("Tests/images/tiny.png") as im:
258
assert im.mode == "P"
260
im_pa = im.convert("PA")
261
assert im_pa.mode == "PA"
263
im_a = im_pa.getchannel("A")
265
alpha = 255 if x > 1 else 0
267
assert im_a.getpixel((x, y)) == alpha
270
def test_p2pa_palette() -> None:
271
with Image.open("Tests/images/tiny.png") as im:
272
im_pa = im.convert("PA")
273
assert im_pa.getpalette() == im.getpalette()
276
def test_matrix_illegal_conversion() -> None:
281
0.412453, 0.357580, 0.180423, 0,
282
0.212671, 0.715160, 0.072169, 0,
283
0.019334, 0.119193, 0.950227, 0)
285
assert im.mode != "RGB"
288
with pytest.raises(ValueError):
289
im.convert(mode="CMYK", matrix=matrix)
292
def test_matrix_wrong_mode() -> None:
297
0.412453, 0.357580, 0.180423, 0,
298
0.212671, 0.715160, 0.072169, 0,
299
0.019334, 0.119193, 0.950227, 0)
301
assert im.mode == "L"
304
with pytest.raises(ValueError):
305
im.convert(mode="L", matrix=matrix)
308
@pytest.mark.parametrize("mode", ("RGB", "L"))
309
def test_matrix_xyz(mode: str) -> None:
312
im.info["transparency"] = (255, 0, 0)
315
0.412453, 0.357580, 0.180423, 0,
316
0.212671, 0.715160, 0.072169, 0,
317
0.019334, 0.119193, 0.950227, 0)
319
assert im.mode == "RGB"
323
converted_im = im.convert(mode=mode, matrix=matrix)
326
assert converted_im.mode == mode
327
assert converted_im.size == im.size
328
with Image.open("Tests/images/hopper-XYZ.png") as target:
329
if converted_im.mode == "RGB":
330
assert_image_similar(converted_im, target, 3)
331
assert converted_im.info["transparency"] == (105, 54, 4)
333
assert_image_similar(converted_im, target.getchannel(0), 1)
334
assert converted_im.info["transparency"] == 105
337
def test_matrix_identity() -> None:
346
assert im.mode == "RGB"
350
converted_im = im.convert(mode="RGB", matrix=identity_matrix)
354
assert_image_equal(converted_im, im)