1
from __future__ import annotations
5
from pathlib import Path
9
from PIL import Image, PpmImagePlugin
13
assert_image_equal_tofile,
19
TEST_FILE = "Tests/images/hopper.ppm"
22
def test_sanity() -> None:
23
with Image.open(TEST_FILE) as im:
24
assert im.mode == "RGB"
25
assert im.size == (128, 128)
26
assert im.format == "PPM"
27
assert im.get_format_mimetype() == "image/x-portable-pixmap"
30
@pytest.mark.parametrize(
33
(b"P2 3 1 4 0 2 4", "L", (0, 128, 255)),
34
(b"P2 3 1 257 0 128 257", "I", (0, 32640, 65535)),
35
# P3 with maxval < 255
37
b"P3 3 1 17 0 1 2 8 9 10 15 16 17",
39
((0, 15, 30), (120, 135, 150), (225, 240, 255)),
41
# P3 with maxval > 255
42
# Scale down to 255, since there is no RGB mode with more than 8-bit
44
b"P3 3 1 257 0 1 2 128 129 130 256 257 257",
46
((0, 1, 2), (127, 128, 129), (254, 255, 255)),
48
(b"P5 3 1 4 \x00\x02\x04", "L", (0, 128, 255)),
49
(b"P5 3 1 257 \x00\x00\x00\x80\x01\x01", "I", (0, 32640, 65535)),
50
# P6 with maxval < 255
52
b"P6 3 1 17 \x00\x01\x02\x08\x09\x0A\x0F\x10\x11",
60
# P6 with maxval > 255
62
b"P6 3 1 257 \x00\x00\x00\x01\x00\x02"
63
b"\x00\x80\x00\x81\x00\x82\x01\x00\x01\x01\xFF\xFF",
73
def test_arbitrary_maxval(
74
data: bytes, mode: str, pixels: tuple[int | tuple[int, int, int], ...]
77
with Image.open(fp) as im:
78
assert im.size == (3, 1)
79
assert im.mode == mode
82
assert tuple(px[x, 0] for x in range(3)) == pixels
85
def test_16bit_pgm() -> None:
86
with Image.open("Tests/images/16_bit_binary.pgm") as im:
88
assert im.size == (20, 100)
89
assert im.get_format_mimetype() == "image/x-portable-graymap"
91
assert_image_equal_tofile(im, "Tests/images/16_bit_binary_pgm.tiff")
94
def test_16bit_pgm_write(tmp_path: Path) -> None:
95
with Image.open("Tests/images/16_bit_binary.pgm") as im:
96
filename = str(tmp_path / "temp.pgm")
97
im.save(filename, "PPM")
99
assert_image_equal_tofile(im, filename)
102
def test_pnm(tmp_path: Path) -> None:
103
with Image.open("Tests/images/hopper.pnm") as im:
104
assert_image_similar(im, hopper(), 0.0001)
106
filename = str(tmp_path / "temp.pnm")
109
assert_image_equal_tofile(im, filename)
112
def test_pfm(tmp_path: Path) -> None:
113
with Image.open("Tests/images/hopper.pfm") as im:
114
assert im.info["scale"] == 1.0
115
assert_image_equal(im, hopper("F"))
117
filename = str(tmp_path / "tmp.pfm")
120
assert_image_equal_tofile(im, filename)
123
def test_pfm_big_endian(tmp_path: Path) -> None:
124
with Image.open("Tests/images/hopper_be.pfm") as im:
125
assert im.info["scale"] == 2.5
126
assert_image_equal(im, hopper("F"))
128
filename = str(tmp_path / "tmp.pfm")
131
assert_image_equal_tofile(im, filename)
134
@pytest.mark.parametrize(
137
b"Pf 1 1 NaN \0\0\0\0",
138
b"Pf 1 1 inf \0\0\0\0",
139
b"Pf 1 1 -inf \0\0\0\0",
140
b"Pf 1 1 0.0 \0\0\0\0",
141
b"Pf 1 1 -0.0 \0\0\0\0",
144
def test_pfm_invalid(data: bytes) -> None:
145
with pytest.raises(ValueError):
146
with Image.open(BytesIO(data)):
150
@pytest.mark.parametrize(
151
"plain_path, raw_path",
154
"Tests/images/hopper_1bit_plain.pbm", # P1
155
"Tests/images/hopper_1bit.pbm", # P4
158
"Tests/images/hopper_8bit_plain.pgm", # P2
159
"Tests/images/hopper_8bit.pgm", # P5
162
"Tests/images/hopper_8bit_plain.ppm", # P3
163
"Tests/images/hopper_8bit.ppm", # P6
167
def test_plain(plain_path: str, raw_path: str) -> None:
168
with Image.open(plain_path) as im:
169
assert_image_equal_tofile(im, raw_path)
172
def test_16bit_plain_pgm() -> None:
173
# P2 with maxval 2 ** 16 - 1
174
with Image.open("Tests/images/hopper_16bit_plain.pgm") as im:
175
assert im.mode == "I"
176
assert im.size == (128, 128)
177
assert im.get_format_mimetype() == "image/x-portable-graymap"
179
# P5 with maxval 2 ** 16 - 1
180
assert_image_equal_tofile(im, "Tests/images/hopper_16bit.pgm")
183
@pytest.mark.parametrize(
184
"header, data, comment_count",
186
(b"P1\n2 2", b"1010", 10**6),
187
(b"P2\n3 1\n4", b"0 2 4", 1),
188
(b"P3\n2 2\n255", b"0 0 0 001 1 1 2 2 2 255 255 255", 10**6),
191
def test_plain_data_with_comment(
192
tmp_path: Path, header: bytes, data: bytes, comment_count: int
194
path1 = str(tmp_path / "temp1.ppm")
195
path2 = str(tmp_path / "temp2.ppm")
196
comment = b"# comment" * comment_count
197
with open(path1, "wb") as f1, open(path2, "wb") as f2:
198
f1.write(header + b"\n\n" + data)
199
f2.write(header + b"\n" + comment + b"\n" + data + comment)
201
with Image.open(path1) as im:
202
assert_image_equal_tofile(im, path2)
205
@pytest.mark.parametrize("data", (b"P1\n128 128\n", b"P3\n128 128\n255\n"))
206
def test_plain_truncated_data(tmp_path: Path, data: bytes) -> None:
207
path = str(tmp_path / "temp.ppm")
208
with open(path, "wb") as f:
211
with Image.open(path) as im:
212
with pytest.raises(ValueError):
216
@pytest.mark.parametrize("data", (b"P1\n128 128\n1009", b"P3\n128 128\n255\n100A"))
217
def test_plain_invalid_data(tmp_path: Path, data: bytes) -> None:
218
path = str(tmp_path / "temp.ppm")
219
with open(path, "wb") as f:
222
with Image.open(path) as im:
223
with pytest.raises(ValueError):
227
@pytest.mark.parametrize(
230
b"P3\n128 128\n255\n012345678910", # half token too long
231
b"P3\n128 128\n255\n012345678910 0", # token too long
234
def test_plain_ppm_token_too_long(tmp_path: Path, data: bytes) -> None:
235
path = str(tmp_path / "temp.ppm")
236
with open(path, "wb") as f:
239
with Image.open(path) as im:
240
with pytest.raises(ValueError):
244
def test_plain_ppm_value_negative(tmp_path: Path) -> None:
245
path = str(tmp_path / "temp.ppm")
246
with open(path, "wb") as f:
247
f.write(b"P3\n128 128\n255\n-1")
249
with Image.open(path) as im:
250
with pytest.raises(ValueError, match="Channel value is negative"):
254
def test_plain_ppm_value_too_large(tmp_path: Path) -> None:
255
path = str(tmp_path / "temp.ppm")
256
with open(path, "wb") as f:
257
f.write(b"P3\n128 128\n255\n256")
259
with Image.open(path) as im:
260
with pytest.raises(ValueError, match="Channel value too large"):
264
def test_magic() -> None:
265
with pytest.raises(SyntaxError):
266
PpmImagePlugin.PpmImageFile(fp=BytesIO(b"PyInvalid"))
269
def test_header_with_comments(tmp_path: Path) -> None:
270
path = str(tmp_path / "temp.ppm")
271
with open(path, "wb") as f:
272
f.write(b"P6 #comment\n#comment\r12#comment\r8\n128 #comment\n255\n")
274
with Image.open(path) as im:
275
assert im.size == (128, 128)
278
def test_non_integer_token(tmp_path: Path) -> None:
279
path = str(tmp_path / "temp.ppm")
280
with open(path, "wb") as f:
283
with pytest.raises(ValueError):
284
with Image.open(path):
288
def test_header_token_too_long(tmp_path: Path) -> None:
289
path = str(tmp_path / "temp.ppm")
290
with open(path, "wb") as f:
291
f.write(b"P6\n 01234567890")
293
with pytest.raises(ValueError) as e:
294
with Image.open(path):
297
assert str(e.value) == "Token too long in file header: 01234567890"
300
def test_truncated_file(tmp_path: Path) -> None:
302
path = str(tmp_path / "temp.pgm")
303
with open(path, "wb") as f:
306
with pytest.raises(ValueError) as e:
307
with Image.open(path):
310
assert str(e.value) == "Reached EOF while reading header"
312
# Test EOF for PyDecoder
313
fp = BytesIO(b"P5 3 1 4")
314
with Image.open(fp) as im:
315
with pytest.raises(ValueError):
319
def test_not_enough_image_data(tmp_path: Path) -> None:
320
path = str(tmp_path / "temp.ppm")
321
with open(path, "wb") as f:
322
f.write(b"P2 1 2 255 255")
324
with Image.open(path) as im:
325
with pytest.raises(ValueError):
329
@pytest.mark.parametrize("maxval", (b"0", b"65536"))
330
def test_invalid_maxval(maxval: bytes, tmp_path: Path) -> None:
331
path = str(tmp_path / "temp.ppm")
332
with open(path, "wb") as f:
333
f.write(b"P6\n3 1 " + maxval)
335
with pytest.raises(ValueError) as e:
336
with Image.open(path):
339
assert str(e.value) == "maxval must be greater than 0 and less than 65536"
342
def test_neg_ppm() -> None:
343
# Storage.c accepted negative values for xsize, ysize. the
344
# internal open_ppm function didn't check for sanity but it
345
# has been removed. The default opener doesn't accept negative
348
with pytest.raises(OSError):
349
with Image.open("Tests/images/negative_size.ppm"):
353
def test_mimetypes(tmp_path: Path) -> None:
354
path = str(tmp_path / "temp.pgm")
356
with open(path, "wb") as f:
357
f.write(b"P4\n128 128\n255")
358
with Image.open(path) as im:
359
assert im.get_format_mimetype() == "image/x-portable-bitmap"
361
with open(path, "wb") as f:
362
f.write(b"PyCMYK\n128 128\n255")
363
with Image.open(path) as im:
364
assert im.get_format_mimetype() == "image/x-portable-anymap"
367
@pytest.mark.parametrize("buffer", (True, False))
368
def test_save_stdout(buffer: bool) -> None:
369
old_stdout = sys.stdout
374
mystdout: MyStdOut | BytesIO = MyStdOut() if buffer else BytesIO()
376
sys.stdout = mystdout
378
with Image.open(TEST_FILE) as im:
379
im.save(sys.stdout, "PPM")
382
sys.stdout = old_stdout
384
if isinstance(mystdout, MyStdOut):
385
mystdout = mystdout.buffer
386
with Image.open(mystdout) as reloaded:
387
assert_image_equal_tofile(reloaded, TEST_FILE)