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
Built Distributions
File details
Details for the file webp-0.4.0.tar.gz
.
File metadata
- Download URL: webp-0.4.0.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9265565839ac938327705c5fbfa3d8ceeeb9b5a2c1c3367231caa0bad29464c7 |
|
MD5 | 6e4fa226554b98bf83093a5dad2893f4 |
|
BLAKE2b-256 | 07aa2a64b903367eb3be5e0b5f89e6e6e54162281712d3a0512dbdfaf03ea97f |
File details
Details for the file webp-0.4.0-cp38-abi3-win_arm64.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-win_arm64.whl
- Upload date:
- Size: 189.7 kB
- Tags: CPython 3.8+, Windows ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed479890ca954404925e99c245fb8e456903ab5e17884081304f64d4434763e2 |
|
MD5 | 214b0b51e0b5e58902f86aac023ca6c6 |
|
BLAKE2b-256 | a3ffe7e4245e8cdeee42a7e666bd80e0e48b255405508ae87997640f73b13384 |
File details
Details for the file webp-0.4.0-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 218.2 kB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c5f7718a3e9e19018ed31765273ff323bdf5820d8255d0001d66ea9c3489d0a |
|
MD5 | 61775ca8f517367dc537adb010ec0725 |
|
BLAKE2b-256 | 638d9a79d73249c63e0d216c832697d76072c9f2b0cb09792618972ba9fe3133 |
File details
Details for the file webp-0.4.0-cp38-abi3-musllinux_1_1_x86_64.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 412.2 kB
- Tags: CPython 3.8+, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 016bbea5bc61708708873ecd75dbb29552d28e6bd9180b4e1352d3b1d6811f01 |
|
MD5 | cb6683a769b69f977d5888547f9e8736 |
|
BLAKE2b-256 | c07178751690f32c8bf80d19d2809b4faca5e51c13bcee0a3139941692cd858e |
File details
Details for the file webp-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 405.1 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca6aa8cdff582794146dfecd24aeb2b7986559cca5a1d12b504ffca6d87417d4 |
|
MD5 | 2b2f35c35f4aa9361f94ee8c4ccb7fb6 |
|
BLAKE2b-256 | ceb335269831a40129b52091e4f8837d6a07c0063221e92805203ac16b9426ca |
File details
Details for the file webp-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 356.9 kB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a58a04d74c3323a760cf1b581f5c0ea36f8df426065f434c5fb4c7eb06d2e26 |
|
MD5 | 63f2c4349227b4238b392c6488781f05 |
|
BLAKE2b-256 | f75ca99e30271160a1a5eaa9a886c882cf8a8f4db178b63e878523e768dc364d |
File details
Details for the file webp-0.4.0-cp38-abi3-macosx_10_9_universal2.whl
.
File metadata
- Download URL: webp-0.4.0-cp38-abi3-macosx_10_9_universal2.whl
- Upload date:
- Size: 605.5 kB
- Tags: CPython 3.8+, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 697f5180df0f6ea70659872f835d4727009492e5d9550772a562e559fc06fc75 |
|
MD5 | b22976146c3b0b6c04fa4022419332c4 |
|
BLAKE2b-256 | 81b45772851d64a5be2ea711d3fca988742d5f9ce28cf6c13fb8e4ed8a4909d9 |