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
- libwebp (tested with v1.0.3)
- Install libwebpmux and libwebpdemux components as well.
- Check out the Dockerfile for steps to build from source on Ubuntu.
- Python 3 (tested with v3.6)
- cffi
- Pillow
- numpy
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
Running tests
$ pytest tests/
Cutting releases
Source release:
$ pip3 install build twine
$ python3 -m build --sdist
$ twine upload dist/webp-*.tar.gz
Linux binary wheel release (repeat for different Python versions and architectures):
$ docker run -it -v `pwd`:/io -w /io quay.io/pypa/manylinux2014_x86_64 ./build_manylinux_wheels.sh
$ twine upload dist/webp-*-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
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.3.tar.gz
(10.5 kB
view hashes)
Built Distributions
Close
Hashes for webp-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44450c4e590825ce20277e307ef8b19bc66d88a281b6f06b81e8690740d87334 |
|
MD5 | e21d6a49c00c718b7a8e12cbf5611fb5 |
|
BLAKE2b-256 | c016cbf0f65b24af0028960e5543cb7fedf3bc2747980025d06acf06a4d245ad |
Close
Hashes for webp-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a434150ef081a61005a684642eb0c7f07a15e570eddeef3204af509806dda111 |
|
MD5 | 0d1eeac63f82f6f6e7630176317cf52e |
|
BLAKE2b-256 | b6561f210565e5bf02cc97a96d55b0fb734a6914c92c3612808fbd6a1233cf53 |
Close
Hashes for webp-0.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5edcae52a9cac54b6cdc641490f51382fc66fa25fbd18a7c4bb2acc1ca5fd3d0 |
|
MD5 | 8e1c3baf8eae664bc6749979e5ba46b7 |
|
BLAKE2b-256 | d68039fffcb9e6333aaa4683317d80acdcdddb6682284d014bf07822d4e11b89 |
Close
Hashes for webp-0.1.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f37351426029dca649e397a722c4b6a5d5869c05f741fa70f57021f4f35bcefe |
|
MD5 | e1b69985c430b78f4497c2655ef60655 |
|
BLAKE2b-256 | 953acdd9b73b8c3bea1955230b02169f9239d8093ec5b1732eb7faed70166d26 |