|
| 1 | +import io |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | from PIL import Image, WebPImagePlugin |
3 | 5 |
|
@@ -54,72 +56,65 @@ def test_read_rgb(self): |
54 | 56 | # dwebp -ppm ../../Tests/images/hopper.webp -o hopper_webp_bits.ppm |
55 | 57 | assert_image_similar_tofile(image, "Tests/images/hopper_webp_bits.ppm", 1.0) |
56 | 58 |
|
57 | | - def test_write_rgb(self, tmp_path): |
58 | | - """ |
59 | | - Can we write a RGB mode file to webp without error. |
60 | | - Does it have the bits we expect? |
61 | | - """ |
62 | | - |
| 59 | + def _roundtrip(self, tmp_path, mode, epsilon, args={}): |
63 | 60 | temp_file = str(tmp_path / "temp.webp") |
64 | 61 |
|
65 | | - hopper(self.rgb_mode).save(temp_file) |
| 62 | + hopper(mode).save(temp_file, **args) |
66 | 63 | with Image.open(temp_file) as image: |
67 | 64 | assert image.mode == self.rgb_mode |
68 | 65 | assert image.size == (128, 128) |
69 | 66 | assert image.format == "WEBP" |
70 | 67 | image.load() |
71 | 68 | image.getdata() |
72 | 69 |
|
73 | | - # generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm |
74 | | - assert_image_similar_tofile( |
75 | | - image, "Tests/images/hopper_webp_write.ppm", 12.0 |
76 | | - ) |
| 70 | + if mode == self.rgb_mode: |
| 71 | + # generated with: dwebp -ppm temp.webp -o hopper_webp_write.ppm |
| 72 | + assert_image_similar_tofile( |
| 73 | + image, "Tests/images/hopper_webp_write.ppm", 12.0 |
| 74 | + ) |
77 | 75 |
|
78 | 76 | # This test asserts that the images are similar. If the average pixel |
79 | 77 | # difference between the two images is less than the epsilon value, |
80 | 78 | # then we're going to accept that it's a reasonable lossy version of |
81 | | - # the image. The old lena images for WebP are showing ~16 on |
82 | | - # Ubuntu, the jpegs are showing ~18. |
83 | | - target = hopper(self.rgb_mode) |
84 | | - assert_image_similar(image, target, 12.0) |
| 79 | + # the image. |
| 80 | + target = hopper(mode) |
| 81 | + if mode != self.rgb_mode: |
| 82 | + target = target.convert(self.rgb_mode) |
| 83 | + assert_image_similar(image, target, epsilon) |
| 84 | + |
| 85 | + def test_write_rgb(self, tmp_path): |
| 86 | + """ |
| 87 | + Can we write a RGB mode file to webp without error? |
| 88 | + Does it have the bits we expect? |
| 89 | + """ |
| 90 | + |
| 91 | + self._roundtrip(tmp_path, self.rgb_mode, 12.5) |
| 92 | + |
| 93 | + def test_write_method(self, tmp_path): |
| 94 | + self._roundtrip(tmp_path, self.rgb_mode, 12.0, {"method": 6}) |
| 95 | + |
| 96 | + buffer_no_args = io.BytesIO() |
| 97 | + hopper().save(buffer_no_args, format="WEBP") |
| 98 | + |
| 99 | + buffer_method = io.BytesIO() |
| 100 | + hopper().save(buffer_method, format="WEBP", method=6) |
| 101 | + assert buffer_no_args.getbuffer() != buffer_method.getbuffer() |
85 | 102 |
|
86 | 103 | def test_write_unsupported_mode_L(self, tmp_path): |
87 | 104 | """ |
88 | 105 | Saving a black-and-white file to WebP format should work, and be |
89 | 106 | similar to the original file. |
90 | 107 | """ |
91 | 108 |
|
92 | | - temp_file = str(tmp_path / "temp.webp") |
93 | | - hopper("L").save(temp_file) |
94 | | - with Image.open(temp_file) as image: |
95 | | - assert image.mode == self.rgb_mode |
96 | | - assert image.size == (128, 128) |
97 | | - assert image.format == "WEBP" |
98 | | - |
99 | | - image.load() |
100 | | - image.getdata() |
101 | | - target = hopper("L").convert(self.rgb_mode) |
102 | | - |
103 | | - assert_image_similar(image, target, 10.0) |
| 109 | + self._roundtrip(tmp_path, "L", 10.0) |
104 | 110 |
|
105 | 111 | def test_write_unsupported_mode_P(self, tmp_path): |
106 | 112 | """ |
107 | 113 | Saving a palette-based file to WebP format should work, and be |
108 | 114 | similar to the original file. |
109 | 115 | """ |
110 | 116 |
|
111 | | - temp_file = str(tmp_path / "temp.webp") |
112 | | - hopper("P").save(temp_file) |
113 | | - with Image.open(temp_file) as image: |
114 | | - assert image.mode == self.rgb_mode |
115 | | - assert image.size == (128, 128) |
116 | | - assert image.format == "WEBP" |
117 | | - |
118 | | - image.load() |
119 | | - image.getdata() |
120 | | - target = hopper("P").convert(self.rgb_mode) |
121 | | - |
122 | | - assert_image_similar(image, target, 50.0) |
| 117 | + self._roundtrip(tmp_path, "P", 50.0) |
123 | 118 |
|
124 | 119 | def test_WebPEncode_with_invalid_args(self): |
125 | 120 | """ |
|
0 commit comments