Skip to main content

Python interface for libheif library

Project description

pillow-heif

Analysis & Coverage Nightly build Wheels test docs codecov

PythonVersion impl pypi Downloads Downloads

Mac OS Windows Linux Alpine Linux Raspberry Pi

Python bindings to libheif for working with HEIF images and an add-on for Pillow.

Features:

  • Decoding of 8, 10, 12 bit HEIC and AVIF files.
  • Encoding of 8, 10, 12 bit HEIC and AVIF files.
  • EXIF, XMP, IPTC read & write support.
  • Support of multiple images in one file and a PrimaryImage attribute.
  • HEIF thumbnails support.
  • Adding all this features to Pillow in one line of code as a plugin.

Note: Here is a light version pi-heif of this project without encoding capabilities.

Install

python3 -m pip install -U pip
python3 -m pip install pillow-heif

Example of use as a Pillow plugin

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)

16 bit PNG to 10 bit HEIF using OpenCV

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)

8/10/12 bit HEIF to 16 bit PNG using OpenCV

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)

Accessing decoded image data

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)

Get decoded image data as a Numpy array

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)

Adding & Removing thumbnails

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)Adding & Removing thumbnails

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

Using thumbnails when they are present in a file

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.

(Pillow)Using thumbnails when they are present in a file

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 support

Working with the AVIF files as the same as with the HEIC files. Just use a separate function to register plugin:

import pillow_heif

pillow_heif.register_avif_opener()

More Information

Wheels

Wheels table macOS
Intel
macOS
Silicon
Windows
64bit
musllinux* manylinux*
CPython 3.7 N/A
CPython 3.8
CPython 3.9
CPython 3.10
CPython 3.11
PyPy 3.8 v7.3 N/A N/A N/A
PyPy 3.9 v7.3 N/A N/A N/A

* i686, x86_64, aarch64 wheels.

For ARMv7(armv7l) wheels are present for Debian 10+(Ubuntu 20.04+) and Alpine 14+. They supports only decoding and builds without x265 encoder.

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

pillow_heif-0.9.3.tar.gz (8.0 MB view hashes)

Uploaded Source

Built Distributions

pillow_heif-0.9.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pillow_heif-0.9.3-pp39-pypy39_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (834.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

pillow_heif-0.9.3-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pillow_heif-0.9.3-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (6.7 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

pillow_heif-0.9.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.3 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pillow_heif-0.9.3-pp38-pypy38_pp73-manylinux_2_17_i686.manylinux2014_i686.whl (834.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686

pillow_heif-0.9.3-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.5 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pillow_heif-0.9.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (6.7 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

pillow_heif-0.9.3-cp38-abi3-macosx_12_0_arm64.whl (4.2 MB view hashes)

Uploaded CPython 3.8+ macOS 12.0+ ARM64

pillow_heif-0.9.3-cp37-abi3-win_amd64.whl (7.8 MB view hashes)

Uploaded CPython 3.7+ Windows x86-64

pillow_heif-0.9.3-cp37-abi3-musllinux_1_2_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARMv7l

pillow_heif-0.9.3-cp37-abi3-musllinux_1_1_x86_64.whl (8.0 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ x86-64

pillow_heif-0.9.3-cp37-abi3-musllinux_1_1_i686.whl (1.5 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ i686

pillow_heif-0.9.3-cp37-abi3-musllinux_1_1_aarch64.whl (7.1 MB view hashes)

Uploaded CPython 3.7+ musllinux: musl 1.1+ ARM64

pillow_heif-0.9.3-cp37-abi3-manylinux_2_28_armv7l.whl (670.7 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.28+ ARMv7l

pillow_heif-0.9.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.5 MB view hashes)

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

pillow_heif-0.9.3-cp37-abi3-manylinux_2_17_i686.manylinux2014_i686.whl (985.8 kB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ i686

pillow_heif-0.9.3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.7 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

pillow_heif-0.9.3-cp37-abi3-macosx_10_9_x86_64.whl (6.7 MB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ 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