|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import itertools |
| 4 | + |
3 | 5 | import pytest |
4 | 6 |
|
5 | 7 | from PIL import Image |
@@ -44,7 +46,24 @@ def test_equal(mode): |
44 | 46 | assert img_a.im == img_b.im |
45 | 47 |
|
46 | 48 |
|
47 | | -@pytest.mark.parametrize("mode", Image.MODES) |
| 49 | +# With mode "1" different bytes can map to the same value, |
| 50 | +# so we have to be more specific with the values we use. |
| 51 | +@pytest.mark.parametrize( |
| 52 | + "bytes_a, bytes_b", |
| 53 | + itertools.permutations( |
| 54 | + (bytes(x) for x in itertools.product(b"\x00\xff", repeat=4)), 2 |
| 55 | + ), |
| 56 | +) |
| 57 | +def test_not_equal_mode_1(bytes_a, bytes_b): |
| 58 | + # Use rawmode "1;8" so that each full byte is interpreted as a value |
| 59 | + # instead of the bits in the bytes being interpreted as values. |
| 60 | + img_a = Image.frombytes("1", (2, 2), bytes_a, "raw", "1;8") |
| 61 | + img_b = Image.frombytes("1", (2, 2), bytes_b, "raw", "1;8") |
| 62 | + assert img_a.tobytes() != img_b.tobytes() |
| 63 | + assert img_a.im != img_b.im |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("mode", [mode for mode in Image.MODES if mode != "1"]) |
48 | 67 | def test_not_equal(mode): |
49 | 68 | num_img_bytes = len(Image.new(mode, (2, 2)).tobytes()) |
50 | 69 | data_a = bytes(range(ord("A"), ord("A") + num_img_bytes)) |
|
0 commit comments