Python 3.6+ 到 libheif 库的接口
项目描述
枕头海夫
Python 绑定到libheif以处理 HEIF 图像和 Pillow 的附加组件。
特征:
8,10,12位 HEIC 和 AVIF 文件的解码。8,10,12bit HEIC 和 AVIF 文件的编码。EXIF,XMP,IPTC读写支持。- 支持一个文件和一个
PrimaryImage属性中的多个图像。 - HEIF
thumbnails支持。 - 在一行代码中将所有这些功能作为插件添加到 Pillow。
安装
python3 -m pip install -U pip
python3 -m pip install pillow-heif
用作枕头插件的示例
from PIL import Image
from pillow_heif import register_heif_opener
register_heif_opener()
im = Image.open("images/input.heic") # do whatever need with a Pillow image
im = im.rotate(13)
im.save(f"rotated_image.heic", quality=90)
使用 OpenCV 将 16 位 PNG 转换为 10 位 HEIF
import cv2
import pillow_heif
cv_img = cv2.imread("images/jpeg_gif_png/RGBA_16.png", cv2.IMREAD_UNCHANGED)
heif_file = pillow_heif.from_bytes(
mode="BGRA;16",
size=(cv_img.shape[1], cv_img.shape[0]),
data=bytes(cv_img)
)
heif_file.save("RGBA_10bit.heic", quality=-1)
使用 OpenCV 将 8/10/12 位 HEIF 转换为 16 位 PNG
import numpy as np
import cv2
import pillow_heif
heif_file = pillow_heif.open_heif("images/rgb12.heif", convert_hdr_to_8bit=False)
heif_file.convert_to("BGRA;16" if heif_file.has_alpha else "BGR;16")
np_array = np.asarray(heif_file)
cv2.imwrite("rgb16.png", np_array)
访问解码的图像数据
import pillow_heif
if pillow_heif.is_supported("images/rgb10.heif"):
heif_file = pillow_heif.open_heif("images/rgb10.heif", convert_hdr_to_8bit=False)
print("image mode:", heif_file.mode)
print("image data length:", len(heif_file.data))
print("image data stride:", heif_file.stride)
heif_file.convert_to("RGB;16") # convert 10 bit image to RGB 16 bit.
print("image mode:", heif_file.mode)
以 Numpy 数组的形式获取解码的图像数据
import numpy as np
import pillow_heif
if pillow_heif.is_supported("input.heic"):
heif_file = pillow_heif.open_heif("input.heic")
np_array = np.asarray(heif_file)
添加和删除缩略图
import pillow_heif
if pillow_heif.is_supported("input.heic"):
heif_file = pillow_heif.open_heif("input.heic")
pillow_heif.add_thumbnails(heif_file, [768, 512, 256]) # add three new thumbnail boxes.
heif_file.save("output_with_thumbnails.heic")
heif_file.thumbnails.clear() # clear list with thumbnails.
heif_file.save("output_without_thumbnails.heic")
(Pillow)添加和删除缩略图
from PIL import Image
import pillow_heif
pillow_heif.register_heif_opener()
im = Image.open("input.heic")
pillow_heif.add_thumbnails(im, [768, 512, 256]) # add three new thumbnail boxes.
im.save("output_with_thumbnails.heic")
im.info["thumbnails"].clear() # clear list with thumbnails.
im.save("output_without_thumbnails.heic")
当缩略图出现在文件中时使用它们
import pillow_heif
if pillow_heif.is_supported("input.heic"):
heif_file = pillow_heif.open_heif("input.heic")
for img in heif_file:
img = pillow_heif.thumbnail(img)
print(img) # This will be a thumbnail or if thumbnail is not avalaible then an original.
(枕头)在文件中存在缩略图时使用缩略图
from PIL import Image, ImageSequence
import pillow_heif
pillow_heif.register_heif_opener()
pil_img = Image.open("input.heic")
for img in ImageSequence.Iterator(pil_img):
img = pillow_heif.thumbnail(img)
print(img) # This will be a thumbnail or if thumbnail is not avalaible then an original.
AVIF 支持
处理文件与处理文件AVIF一样HEIC。只需使用单独的函数来注册插件:
import pillow_heif
pillow_heif.register_avif_opener()
更多信息
轮子
| 轮子表 | macOS 英特尔 |
macOS 硅 |
视窗 64 位 |
musllinux* | 许多Linux* |
|---|---|---|---|---|---|
| CPython 3.6 | 不适用 | 不适用 | 不适用 | ✅ | ✅ |
| CPython 3.7 | ✅ | 不适用 | ✅ | ✅ | ✅ |
| CPython 3.8 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.9 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.10 | ✅ | ✅ | ✅ | ✅ | ✅ |
| CPython 3.11 | ✅ | ✅ | ✅ | ✅ | ✅ |
| PyPy 3.7 v7.3 | ✅ | 不适用 | 不适用 | 不适用 | ✅ |
| PyPy 3.8 v7.3 | ✅ | 不适用 | 不适用 | 不适用 | ✅ |
* i686、x86_64、aarch64轮子。
因为Debian11+ 系统armv7l有一个pillow_heif-x.x.x-cp38-abi3-manylinux_2_31_armv7l.whl轮子。pypi它只支持解码和不带x265编码器的构建。