from PIL import features
from PIL import Image 

#检查是否支持 webp
print(features.check_module('webp'))

#检查是否支持 animated webp 
print(features.check_module('webp_anim'))


#JPG -> PNG
img_pil = Image.open('test.jpg')
img_pil.convert('RGB').save('test.png', 'png')

#PNG -> JPG
img_pil = Image.open('test.png')
img_pil.convert('RGB').save('test.jpg', 'jpeg')

#JPG -> Webp
img_pil = Image.open('test.jpg')
img_pil.convert('RGB').save('test.webp', 'webp')
#img_pil.save('test.webp', 'webp', optimize = True, quality = 10)

#Webp -> JPG
img_pil = Image.open('test.webp')
img_pil.convert('RGB').save('test.jpg', 'jpeg')

#PNG -> Webp
img_pil = Image.open('test.png')
img_pil.save('test.webp', 'webp')
#img_pil.save('test.webp', 'webp', lossless = True)

#Webp -> PNG
img_pil = Image.open('test.webp')
img_pil.save('test.png', 'png')

#GIF -> Animated PNG
#提取 gif 每一帧图片
sequence = []
im = Image.open('gif-test.gif')
for frame in ImageSequence.Iterator(im):
    sequence.append(frame.copy())
#保存 Animated PNG
sequence[0].save('gif-test.webp', save_all=True, append_images = sequence[1:])

pywebp

#!/usr/bin/python3
#!--*-- coding: utf-8 --*--
'''
pip install webp
# libwebp
# cffi
'''

import webp

# 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)

#numpy arrays
# Save an image
webp.imwrite(img, 'image.webp', quality=80)
# Load an image
img = webp.imread('image.webp', 'RGBA')
# Save an animation
webp.mimwrite(imgs, 'anim.webp', fps=10, lossless=True)
# Load an animation
imgs = webp.mimread('anim.webp', 'RGB', fps=10)


#Advaced 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

Linux

Linux 支持命令行将 Webp 图像转换为 PNG 和 JPEG.

#sudo apt-get install webp

# JPEG/PNG -> Webp 
#命令行格式:cwebp -q [image_quality] [JPEG/PNG_filename] -o [WebP_filename]
cwebp -q 90 test.jpeg -o test.webp

#Webp -> JPEG/PNG
#命令行格式:dwebp [WebP_filename] -o [PNG_filename]
dwebp test.webp -o test.png
Last modification:December 20th, 2021 at 11:52 am