Python bindings for WebP
Project description
WebP Python bindings
Installation
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.
Requirements
- Python 3.8+
Usage
import webp
Simple API
# 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.
Advanced API
# 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
Features
- Picture encoding/decoding
- Animation encoding/decoding
- Automatic memory management
- Simple API for working with
PIL.Image
objects
Not implemented
- Encoding/decoding still images in YUV color mode
- Advanced muxing/demuxing (color profiles, etc.)
- Expose all useful fields
Developer notes
Setting up
- Install
mamba
andconda-lock
. The easiest way to do this is by installing Mambaforge and then runningmamba install conda-lock
. - Create and activate the Conda environment:
$ conda-lock install -n webp $ mamba activate webp
- Install PyPI dependencies:
$ pdm install -G:all
Running tests
$ pytest tests/
Cutting a new release
- Ensure that tests are passing and everything is ready for release.
- Create and push a Git tag:
$ git tag v0.1.6 $ git push --tags
- Download the artifacts from GitHub Actions, which will include the source distribution tarball and binary wheels.
- Create a new release on GitHub from the tagged commit and upload the packages as attachments to the release.
- Also upload the packages to PyPI using Twine:
$ twine upload webp-*.tar.gz webp-*.whl
- Bump the version number in
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.
Known issues
- An animation where all frames are identical will "collapse" in on itself,
resulting in a single frame. Unfortunately, WebP seems to discard timestamp
information in this case, which breaks
webp.load_images
when the FPS is specified. - There are currently no 32-bit binaries of libwebp uploaded to Conan Center. If you are running 32-bit Python, libwebp will be built from source.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
webp-0.1.7.tar.gz
(17.6 kB
view hashes)
Built Distributions
webp-0.1.7-cp311-cp311-win_amd64.whl
(258.8 kB
view hashes)
webp-0.1.7-cp310-cp310-win_amd64.whl
(258.8 kB
view hashes)
webp-0.1.7-cp39-cp39-win_amd64.whl
(258.8 kB
view hashes)
webp-0.1.7-cp38-cp38-win_amd64.whl
(258.8 kB
view hashes)
Close
Hashes for webp-0.1.7-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c2fe38aefa6f33cdb98919f62e6b1fe41e660f32d5f7f88b083cf58663b74f9 |
|
MD5 | 4619bfeb97df14e53e13619179a4b364 |
|
BLAKE2b-256 | dd6867ef07465fb807111814fd6b803c2cffd1f329874be936a904007fa24c53 |
Close
Hashes for webp-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b4b3e922bb388fb078cf7565665d170bdc514dd1b7196e773d7af73c2c5748d |
|
MD5 | ff29cba38fe9fa5b47cc4972d6b6d3a9 |
|
BLAKE2b-256 | 85e6bdb6445bca2de31570595c076fe4a81bdf1888148c121e329a7dbf2785ab |
Close
Hashes for webp-0.1.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e2148fdfcb86046a8a57b5f7e61255e670372942267ba24069cda9539022a8d |
|
MD5 | c3eaaa271c27331e8a632839c55b48b6 |
|
BLAKE2b-256 | 2c08635bbe918ce950b2b6e9d8179cb130b1329cf9af1d4466e88dd723623c53 |
Close
Hashes for webp-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94cb0247d9bdac06d226d286c428b2ab353bcfde1585ca2279bad663f53c96e7 |
|
MD5 | e6a256e254ebbfebbac61ed9df1c1b61 |
|
BLAKE2b-256 | d2d789d3506871d01f90dadaf55fecc3b122f0eb5a014201020066f5169f09ed |
Close
Hashes for webp-0.1.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66a3448a129b55d40b853c5b34a94a6de35a370eed5126d77959e8c5e62d51ce |
|
MD5 | 05ef3c8210746ed35ceeae408b3f6c91 |
|
BLAKE2b-256 | a25c44601c5da0fbb1f178fdffb4060b3424ea9d07c54366a05e9c441b3c1b99 |
Close
Hashes for webp-0.1.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b3dee1288987a766fe9765cdadbe388bbee5f1ca37482d18f895559fe5d4f6f |
|
MD5 | 4bdc94bd0873357368d082dbf0cd44ad |
|
BLAKE2b-256 | 182713a8d4177d33112cd22103a91b6b79145e1b39f9ecb7b8ed5856f7b24f40 |
Close
Hashes for webp-0.1.7-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d64bfe26fbd390fc89afd878adf9a66582a3f96f37fe49b0d2485823ad7240e2 |
|
MD5 | c2a59b39890b2cdd46320094c201b52b |
|
BLAKE2b-256 | 612440f4ccb4dd3b12150a47da9c42b48891397f46c1a8658444a1d692d95b06 |
Close
Hashes for webp-0.1.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fa31c2bc12adac088e00db0d20423e658d9d2a4312f3f07b7674d55798573b1 |
|
MD5 | be510e28986fa208dcb60875634fb8e6 |
|
BLAKE2b-256 | 0b486dd20a88ba9e159642f070f0c175c4c019b5032cc00bfc1e8e109fc55181 |