Skip to main content

Python bindings for WebP

Project description

CircleCI license PyPI GitHub

Usage

Writing a PIL image to a WebP file

pic = webp.WebPPicture.from_pil(img)
buf = pic.encode().buffer()

with open('image.webp', 'wb') as f:
  f.write(buf)

Reading a WebP file to a numpy array

with open('image.webp', 'rb') as f:
  webp_data = webp.WebPData.from_buffer(f.read())
  arr = webp_data.decode() # Defaults to RGBA

Writing 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())

Reading 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

Requirements

  • Python 3 (tested with v3.6)

  • libwebp, libwebpmux, libwebpdemux (tested with v0.6.0)

  • Pillow (tested with v4.1.1)

  • numpy (tested with v1.12.1)

Features

  • Still image encoding/decoding

  • Animation encoding/decoding

  • Automatic memory management

  • Support for PIL.Image and numpy.array objects

Not implemented

  • Encoding/decoding still images in YUV color mode

  • Advanced muxing/demuxing (color profiles, etc.)

  • Expose all useful fields

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.0a4.tar.gz (7.2 kB view hashes)

Uploaded Source

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