|
6 | 6 |
|
7 | 7 | from PIL import Image, PpmImagePlugin |
8 | 8 |
|
9 | | -from .helper import assert_image_equal_tofile, assert_image_similar, hopper |
| 9 | +from .helper import ( |
| 10 | + assert_image_equal, |
| 11 | + assert_image_equal_tofile, |
| 12 | + assert_image_similar, |
| 13 | + hopper, |
| 14 | +) |
10 | 15 |
|
11 | 16 | # sample ppm stream |
12 | 17 | TEST_FILE = "Tests/images/hopper.ppm" |
@@ -84,20 +89,58 @@ def test_16bit_pgm(): |
84 | 89 |
|
85 | 90 | def test_16bit_pgm_write(tmp_path): |
86 | 91 | with Image.open("Tests/images/16_bit_binary.pgm") as im: |
87 | | - f = str(tmp_path / "temp.pgm") |
88 | | - im.save(f, "PPM") |
| 92 | + filename = str(tmp_path / "temp.pgm") |
| 93 | + im.save(filename, "PPM") |
89 | 94 |
|
90 | | - assert_image_equal_tofile(im, f) |
| 95 | + assert_image_equal_tofile(im, filename) |
91 | 96 |
|
92 | 97 |
|
93 | 98 | def test_pnm(tmp_path): |
94 | 99 | with Image.open("Tests/images/hopper.pnm") as im: |
95 | 100 | assert_image_similar(im, hopper(), 0.0001) |
96 | 101 |
|
97 | | - f = str(tmp_path / "temp.pnm") |
98 | | - im.save(f) |
| 102 | + filename = str(tmp_path / "temp.pnm") |
| 103 | + im.save(filename) |
| 104 | + |
| 105 | + assert_image_equal_tofile(im, filename) |
| 106 | + |
| 107 | + |
| 108 | +def test_pfm(tmp_path): |
| 109 | + with Image.open("Tests/images/hopper.pfm") as im: |
| 110 | + assert im.info["scale"] == 1.0 |
| 111 | + assert_image_equal(im, hopper("F")) |
| 112 | + |
| 113 | + filename = str(tmp_path / "tmp.pfm") |
| 114 | + im.save(filename) |
| 115 | + |
| 116 | + assert_image_equal_tofile(im, filename) |
| 117 | + |
| 118 | + |
| 119 | +def test_pfm_big_endian(tmp_path): |
| 120 | + with Image.open("Tests/images/hopper_be.pfm") as im: |
| 121 | + assert im.info["scale"] == 2.5 |
| 122 | + assert_image_equal(im, hopper("F")) |
99 | 123 |
|
100 | | - assert_image_equal_tofile(im, f) |
| 124 | + filename = str(tmp_path / "tmp.pfm") |
| 125 | + im.save(filename) |
| 126 | + |
| 127 | + assert_image_equal_tofile(im, filename) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + "data", |
| 132 | + [ |
| 133 | + b"Pf 1 1 NaN \0\0\0\0", |
| 134 | + b"Pf 1 1 inf \0\0\0\0", |
| 135 | + b"Pf 1 1 -inf \0\0\0\0", |
| 136 | + b"Pf 1 1 0.0 \0\0\0\0", |
| 137 | + b"Pf 1 1 -0.0 \0\0\0\0", |
| 138 | + ], |
| 139 | +) |
| 140 | +def test_pfm_invalid(data): |
| 141 | + with pytest.raises(ValueError): |
| 142 | + with Image.open(BytesIO(data)): |
| 143 | + pass |
101 | 144 |
|
102 | 145 |
|
103 | 146 | @pytest.mark.parametrize( |
|
0 commit comments