Skip to main content

Python bindings for WebP

Project description

WebP Python bindings

Build status License PyPI GitHub

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 a new release

  1. Ensure that tests are passing and everything is ready for release.
  2. Create and push a Git tag:
    $ git tag -a v0.1.4
    $ git push --tags
    
  3. Download the artifacts from GitHub Actions, which will include the source distribution tarball and binary wheels.
  4. Create a new release on GitHub from the tagged commit and upload the packages as attachments to the release.
  5. Also upload the packages to PyPI using Twine:
    $ twine upload webp-*.tar.gz webp-*.whl
    
  6. Bump the version number in setup.cfg 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


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.4.tar.gz (10.4 kB view hashes)

Uploaded Source

Built Distributions

webp-0.1.4-cp310-cp310-win_amd64.whl (260.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

webp-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

webp-0.1.4-cp39-cp39-win_amd64.whl (260.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

webp-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

webp-0.1.4-cp38-cp38-win_amd64.whl (260.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

webp-0.1.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (424.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

webp-0.1.4-cp37-cp37m-win_amd64.whl (260.9 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

webp-0.1.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (423.7 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

webp-0.1.4-cp36-cp36m-win_amd64.whl (261.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

webp-0.1.4-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (423.7 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page