def test_image(self):
img = Image.new('RGB', (32, 16))
draw = ImageDraw.Draw(img)
draw.rectangle([0, 0, 7, 15], fill=(255, 0, 0))
pic = webp.WebPPicture.from_pil(img)
config = webp.WebPConfig.new(lossless=True)
buf = pic.encode(config).buffer()
with TemporaryDirectory() as tmpdir:
file_name = os.path.join(tmpdir, 'image.webp')
with open(file_name, 'wb') as f:
f.write(buf)
with open(file_name, 'rb') as f:
webp_data = webp.WebPData.from_buffer(f.read())
arr = webp_data.decode(color_mode=webp.WebPColorMode.RGB)
expected = np.asarray(img, dtype=np.uint8)
np.testing.assert_array_equal(arr, expected)
评论列表
文章目录