pip install webp
On Windows you may encounter the following error during installation:
conans.errors.ConanException: 'settings.compiler' value not defined
This means that you need to install a C compiler and configure Conan so that it knows which compiler to use. See https://github.com/anibali/pywebp/issues/20 for more details.
import webp
# Save an image
webp.save_image(img, 'image.webp', quality=80)
# Load an image
img = webp.load_image('image.webp', 'RGBA')
# Save an animation
webp.save_images(imgs, 'anim.webp', fps=10, lossless=True)
# Load an animation
imgs = webp.load_images('anim.webp', 'RGB', fps=10)
If you prefer working with numpy arrays, use the functions imwrite
, imread
, mimwrite
,
and mimread
instead.
# Encode a PIL image to WebP in memory, with encoder hints
pic = webp.WebPPicture.from_pil(img)
config = WebPConfig.new(preset=webp.WebPPreset.PHOTO, quality=70)
buf = pic.encode(config).buffer()
# Read a WebP file and decode to a BGR numpy array
with open('image.webp', 'rb') as f:
webp_data = webp.WebPData.from_buffer(f.read())
arr = webp_data.decode(color_mode=WebPColorMode.BGR)
# Save an animation
enc = webp.WebPAnimEncoder.new(width, height)
timestamp_ms = 0
for img in imgs:
pic = webp.WebPPicture.from_pil(img)
enc.encode_frame(pic, timestamp_ms)
timestamp_ms += 250
anim_data = enc.assemble(timestamp_ms)
with open('anim.webp', 'wb') as f:
f.write(anim_data.buffer())
# Load an animation
with open('anim.webp', 'rb') as f:
webp_data = webp.WebPData.from_buffer(f.read())
dec = webp.WebPAnimDecoder.new(webp_data)
for arr, timestamp_ms in dec.frames():
# `arr` contains decoded pixels for the frame
# `timestamp_ms` contains the _end_ time of the frame
pass
PIL.Image
objectsmamba
and conda-lock
. The easiest way to do this is by installing
Mambaforge and then
running mamba install conda-lock
. $ conda-lock install -n webp
$ mamba activate webp
$ pdm install -G:all
$ pytest tests/
$ git tag v0.1.6
$ git push --tags
$ twine upload webp-*.tar.gz webp-*.whl
pyproject.toml
and create a commit, signalling the start of development on the next version.These files should also be added to a GitHub release.
webp.load_images
when the FPS
is specified.