@@ -97,38 +97,33 @@ def test_write_rgba(tmp_path):
97
97
assert_image_similar (image , pil_image , 1.0 )
98
98
99
99
100
- def test_write_rgba_keep_transparent (tmp_path ):
100
+ def test_keep_rgb_values_when_transparent (tmp_path ):
101
101
"""
102
- Can we write a RGBA mode file to WebP while preserving
103
- the transparent RGB without error.
104
- Does it have the bits we expect?
102
+ Saving transparent pixels should retain their original RGB values
103
+ when using the "exact" parameter.
105
104
"""
106
105
107
- temp_output_file = str ( tmp_path / "temp.webp " )
106
+ image = hopper ( "RGB " )
108
107
109
- input_image = hopper ("RGB" )
110
- # make a copy of the image
111
- output_image = input_image .copy ()
112
- # make a single channel image with the same size as input_image
113
- new_alpha = Image .new ("L" , input_image .size , 255 )
114
- # make the left half transparent
115
- new_alpha .paste ((0 ,), (0 , 0 , new_alpha .size [0 ] // 2 , new_alpha .size [1 ]))
116
- # putalpha on output_image
117
- output_image .putalpha (new_alpha )
108
+ # create a copy of the image
109
+ # with the left half transparent
110
+ half_transparent_image = image .copy ()
111
+ new_alpha = Image .new ("L" , (128 , 128 ), 255 )
112
+ new_alpha .paste (0 , (0 , 0 , 64 , 128 ))
113
+ half_transparent_image .putalpha (new_alpha )
118
114
119
- # now save with transparent area preserved.
120
- output_image .save (temp_output_file , "WEBP" , exact = True , lossless = True )
121
- # even though it is lossless, if we don't put exact=True, the transparent
122
- # area will be filled with black (or something more conducive to compression)
115
+ # save with transparent area preserved
116
+ temp_file = str (tmp_path / "temp.webp" )
117
+ half_transparent_image .save (temp_file , exact = True , lossless = True )
123
118
124
- with Image .open (temp_output_file ) as image :
125
- image .load ()
119
+ with Image .open (temp_file ) as reloaded :
120
+ assert reloaded .mode == "RGBA"
121
+ assert reloaded .format == "WEBP"
126
122
127
- assert image .mode == "RGBA"
128
- assert image .format == "WEBP"
129
- image .load ()
130
- image = image .convert ("RGB" )
131
- assert_image_similar (image , input_image , 1.0 )
123
+ # even though it is lossless, if we don't use exact=True
124
+ # in libwebp >= 0.5, the transparent area will be filled with black
125
+ # (or something more conducive to compression)
126
+ assert_image_equal (reloaded .convert ("RGB" ), image )
132
127
133
128
134
129
def test_write_unsupported_mode_PA (tmp_path ):
0 commit comments