|
1 | | -import unittest |
2 | | - |
| 1 | +import pytest |
3 | 2 | from PIL import Image |
4 | 3 |
|
5 | | -from .helper import PillowTestCase, assert_image_equal, assert_image_similar, hopper |
| 4 | +from .helper import assert_image_equal, assert_image_similar, hopper |
| 5 | + |
| 6 | +_webp = pytest.importorskip("PIL._webp", reason="WebP support not installed") |
| 7 | + |
| 8 | + |
| 9 | +def setup_module(): |
| 10 | + if _webp.WebPDecoderBuggyAlpha(): |
| 11 | + pytest.skip("Buggy early version of WebP installed, not testing transparency") |
6 | 12 |
|
7 | | -try: |
8 | | - from PIL import _webp |
9 | | -except ImportError: |
10 | | - _webp = None |
11 | 13 |
|
| 14 | +def test_read_rgba(): |
| 15 | + """ |
| 16 | + Can we read an RGBA mode file without error? |
| 17 | + Does it have the bits we expect? |
| 18 | + """ |
12 | 19 |
|
13 | | -@unittest.skipIf(_webp is None, "WebP support not installed") |
14 | | -class TestFileWebpAlpha(PillowTestCase): |
15 | | - def setUp(self): |
16 | | - if _webp.WebPDecoderBuggyAlpha(self): |
17 | | - self.skipTest( |
18 | | - "Buggy early version of WebP installed, not testing transparency" |
19 | | - ) |
| 20 | + # Generated with `cwebp transparent.png -o transparent.webp` |
| 21 | + file_path = "Tests/images/transparent.webp" |
| 22 | + with Image.open(file_path) as image: |
| 23 | + assert image.mode == "RGBA" |
| 24 | + assert image.size == (200, 150) |
| 25 | + assert image.format == "WEBP" |
| 26 | + image.load() |
| 27 | + image.getdata() |
20 | 28 |
|
21 | | - def test_read_rgba(self): |
22 | | - """ |
23 | | - Can we read an RGBA mode file without error? |
24 | | - Does it have the bits we expect? |
25 | | - """ |
| 29 | + image.tobytes() |
26 | 30 |
|
27 | | - # Generated with `cwebp transparent.png -o transparent.webp` |
28 | | - file_path = "Tests/images/transparent.webp" |
29 | | - with Image.open(file_path) as image: |
30 | | - self.assertEqual(image.mode, "RGBA") |
31 | | - self.assertEqual(image.size, (200, 150)) |
32 | | - self.assertEqual(image.format, "WEBP") |
33 | | - image.load() |
34 | | - image.getdata() |
| 31 | + with Image.open("Tests/images/transparent.png") as target: |
| 32 | + assert_image_similar(image, target, 20.0) |
35 | 33 |
|
36 | | - image.tobytes() |
37 | 34 |
|
38 | | - with Image.open("Tests/images/transparent.png") as target: |
39 | | - assert_image_similar(image, target, 20.0) |
| 35 | +def test_write_lossless_rgb(tmp_path): |
| 36 | + """ |
| 37 | + Can we write an RGBA mode file with lossless compression without error? |
| 38 | + Does it have the bits we expect? |
| 39 | + """ |
40 | 40 |
|
41 | | - def test_write_lossless_rgb(self): |
42 | | - """ |
43 | | - Can we write an RGBA mode file with lossless compression without |
44 | | - error? Does it have the bits we expect? |
45 | | - """ |
| 41 | + temp_file = str(tmp_path / "temp.webp") |
| 42 | + # temp_file = "temp.webp" |
46 | 43 |
|
47 | | - temp_file = self.tempfile("temp.webp") |
48 | | - # temp_file = "temp.webp" |
| 44 | + pil_image = hopper("RGBA") |
49 | 45 |
|
50 | | - pil_image = hopper("RGBA") |
| 46 | + mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128)) |
| 47 | + # Add some partially transparent bits: |
| 48 | + pil_image.paste(mask, (0, 0), mask) |
51 | 49 |
|
52 | | - mask = Image.new("RGBA", (64, 64), (128, 128, 128, 128)) |
53 | | - # Add some partially transparent bits: |
54 | | - pil_image.paste(mask, (0, 0), mask) |
| 50 | + pil_image.save(temp_file, lossless=True) |
55 | 51 |
|
56 | | - pil_image.save(temp_file, lossless=True) |
| 52 | + with Image.open(temp_file) as image: |
| 53 | + image.load() |
57 | 54 |
|
58 | | - with Image.open(temp_file) as image: |
59 | | - image.load() |
| 55 | + assert image.mode == "RGBA" |
| 56 | + assert image.size == pil_image.size |
| 57 | + assert image.format == "WEBP" |
| 58 | + image.load() |
| 59 | + image.getdata() |
60 | 60 |
|
61 | | - self.assertEqual(image.mode, "RGBA") |
62 | | - self.assertEqual(image.size, pil_image.size) |
63 | | - self.assertEqual(image.format, "WEBP") |
64 | | - image.load() |
65 | | - image.getdata() |
| 61 | + assert_image_equal(image, pil_image) |
66 | 62 |
|
67 | | - assert_image_equal(image, pil_image) |
68 | 63 |
|
69 | | - def test_write_rgba(self): |
70 | | - """ |
71 | | - Can we write a RGBA mode file to webp without error. |
72 | | - Does it have the bits we expect? |
73 | | - """ |
| 64 | +def test_write_rgba(tmp_path): |
| 65 | + """ |
| 66 | + Can we write a RGBA mode file to WebP without error. |
| 67 | + Does it have the bits we expect? |
| 68 | + """ |
74 | 69 |
|
75 | | - temp_file = self.tempfile("temp.webp") |
| 70 | + temp_file = str(tmp_path / "temp.webp") |
76 | 71 |
|
77 | | - pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20)) |
78 | | - pil_image.save(temp_file) |
| 72 | + pil_image = Image.new("RGBA", (10, 10), (255, 0, 0, 20)) |
| 73 | + pil_image.save(temp_file) |
79 | 74 |
|
80 | | - if _webp.WebPDecoderBuggyAlpha(self): |
81 | | - return |
| 75 | + if _webp.WebPDecoderBuggyAlpha(): |
| 76 | + return |
82 | 77 |
|
83 | | - with Image.open(temp_file) as image: |
84 | | - image.load() |
| 78 | + with Image.open(temp_file) as image: |
| 79 | + image.load() |
85 | 80 |
|
86 | | - self.assertEqual(image.mode, "RGBA") |
87 | | - self.assertEqual(image.size, (10, 10)) |
88 | | - self.assertEqual(image.format, "WEBP") |
89 | | - image.load() |
90 | | - image.getdata() |
| 81 | + assert image.mode == "RGBA" |
| 82 | + assert image.size == (10, 10) |
| 83 | + assert image.format == "WEBP" |
| 84 | + image.load() |
| 85 | + image.getdata() |
91 | 86 |
|
92 | | - # early versions of webp are known to produce higher deviations: |
93 | | - # deal with it |
94 | | - if _webp.WebPDecoderVersion(self) <= 0x201: |
95 | | - assert_image_similar(image, pil_image, 3.0) |
96 | | - else: |
97 | | - assert_image_similar(image, pil_image, 1.0) |
| 87 | + # Early versions of WebP are known to produce higher deviations: |
| 88 | + # deal with it |
| 89 | + if _webp.WebPDecoderVersion() <= 0x201: |
| 90 | + assert_image_similar(image, pil_image, 3.0) |
| 91 | + else: |
| 92 | + assert_image_similar(image, pil_image, 1.0) |
98 | 93 |
|
99 | | - def test_write_unsupported_mode_PA(self): |
100 | | - """ |
101 | | - Saving a palette-based file with transparency to WebP format |
102 | | - should work, and be similar to the original file. |
103 | | - """ |
104 | 94 |
|
105 | | - temp_file = self.tempfile("temp.webp") |
106 | | - file_path = "Tests/images/transparent.gif" |
| 95 | +def test_write_unsupported_mode_PA(tmp_path): |
| 96 | + """ |
| 97 | + Saving a palette-based file with transparency to WebP format |
| 98 | + should work, and be similar to the original file. |
| 99 | + """ |
| 100 | + |
| 101 | + temp_file = str(tmp_path / "temp.webp") |
| 102 | + file_path = "Tests/images/transparent.gif" |
| 103 | + with Image.open(file_path) as im: |
| 104 | + im.save(temp_file) |
| 105 | + with Image.open(temp_file) as image: |
| 106 | + assert image.mode == "RGBA" |
| 107 | + assert image.size == (200, 150) |
| 108 | + assert image.format == "WEBP" |
| 109 | + |
| 110 | + image.load() |
| 111 | + image.getdata() |
107 | 112 | with Image.open(file_path) as im: |
108 | | - im.save(temp_file) |
109 | | - with Image.open(temp_file) as image: |
110 | | - self.assertEqual(image.mode, "RGBA") |
111 | | - self.assertEqual(image.size, (200, 150)) |
112 | | - self.assertEqual(image.format, "WEBP") |
113 | | - |
114 | | - image.load() |
115 | | - image.getdata() |
116 | | - with Image.open(file_path) as im: |
117 | | - target = im.convert("RGBA") |
118 | | - |
119 | | - assert_image_similar(image, target, 25.0) |
| 113 | + target = im.convert("RGBA") |
| 114 | + |
| 115 | + assert_image_similar(image, target, 25.0) |
0 commit comments