pip install webp在 Windows 系统上,您可能会在安装过程中遇到以下错误:
conans.errors.ConanException: 'settings.compiler' value not defined
这意味着您需要安装一个 C 编译器,并配置 Conan 以便它知道使用哪个编译器。详见 anibali#20 for more details.
- Python 3.8+
import webp# 保存图像
webp.save_image(img, 'image.webp', quality=80)
# 加载图像
img = webp.load_image('image.webp', 'RGBA')
# 保存动画
webp.save_images(imgs, 'anim.webp', fps=10, lossless=True)
# 加载动画
imgs = webp.load_images('anim.webp', 'RGB', fps=10)如果您更喜欢使用numpy数组,请使用函数imwrite、imread、mimwrite和mimread。
# 将PIL图像编码为内存中的WebP,并添加编码器提示
pic = webp.WebPPicture.from_pil(img)
config = WebPConfig.new(preset=webp.WebPPreset.PHOTO, quality=70)
buf = pic.encode(config).buffer()
# 读取WebP文件并将其解码为BGR numpy数组
with open('image.webp', 'rb') as f:
webp_data = webp.WebPData.from_buffer(f.read())
arr = webp_data.decode(color_mode=WebPColorMode.BGR)
# 保存动画
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())
# 加载动画
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` 包含了解码后的帧像素
# `timestamp_ms` 包含了帧的结束时间
pass- 图像编码/解码
- 动画编码/解码
- 自动内存管理
- 用于操作
PIL.Image对象的简单API
- 在YUV色彩模式下编码/解码静态图像
- 高级复用/解复用(色彩配置文件等)
- 暴露所有有用的字段
- Install
mambaandconda-lock. The easiest way to do this is by installing Mambaforge and then runningmamba install conda-lock. 安装mamba和conda-lock。最简单的方法是安装Mambaforge,然后运行mamba install conda-lock。 - 创建并激活Conda环境:
$ conda-lock install -n webp $ mamba activate webp
- 安装PyPI依赖项:
$ pdm install -G:all
$ pytest tests/- 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.tomland create a commit, signalling the start of development on the next version.
These files should also be added to a GitHub release.
- 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_imageswhen 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.