Skip to main content

Python bindings for libheif - HEIC/AVIF/JPEG2000 image codec

Project description

pylibheif

Python bindings for libheif using pybind11.

Features

  • HEIC/HEIF Support: Read and write HEIC images (HEVC/H.265 encoded)
  • AVIF Support: Read and write AVIF images (AV1 encoded)
  • JPEG2000 Support: Read and write JPEG2000 images in HEIF container
  • NumPy Integration: Zero-copy access to image data via Python Buffer Protocol
  • Metadata Support: Read EXIF and XMP metadata from images
  • RAII Resource Management: Automatic resource cleanup with context managers

Supported Formats

Format Decoding Encoding Codec
HEIC (HEVC/H.265) libde265 / x265
AVIF (AV1) DAV1D + AOM
JPEG2000 OpenJPEG

Requirements

  • Python >= 3.12
  • NumPy >= 1.26.0
  • CMake >= 3.15
  • C++17 compatible compiler

System Dependencies

# macOS
brew install openjpeg

# Ubuntu/Debian
sudo apt install libopenjp2-7-dev

Installation

pip install pylibheif

Or with uv:

uv pip install pylibheif

Building from Source

# Clone with submodules
git clone --recursive https://github.com/twn39/pylibheif.git
cd pylibheif

# Install
uv pip install -e .

Usage

Reading HEIC/AVIF Images

Using context manager (recommended):

import pylibheif
import numpy as np

# Open HEIC file with context manager
with pylibheif.HeifContext() as ctx:
    ctx.read_from_file('image.heic')
    
    # Get primary image handle
    handle = ctx.get_primary_image_handle()
    print(f'Image size: {handle.width}x{handle.height}')
    print(f'Has alpha: {handle.has_alpha}')
    
    # Decode to RGB
    img = handle.decode(pylibheif.HeifColorspace.RGB, 
                        pylibheif.HeifChroma.InterleavedRGB)
    
    # Get as NumPy array (zero-copy)
    plane = img.get_plane(pylibheif.HeifChannel.Interleaved, False)
    arr = np.asarray(plane)  # shape: (height, width, 3)

Explicit creation (for more control):

import pylibheif
import numpy as np

# Create context explicitly
ctx = pylibheif.HeifContext()
ctx.read_from_file('image.heic')

handle = ctx.get_primary_image_handle()
img = handle.decode(pylibheif.HeifColorspace.RGB, 
                    pylibheif.HeifChroma.InterleavedRGB)
plane = img.get_plane(pylibheif.HeifChannel.Interleaved, False)
arr = np.asarray(plane)

# Resources are automatically freed when objects go out of scope

Writing HEIC/AVIF Images

import pylibheif
import numpy as np

# Create image from NumPy array
width, height = 1920, 1080
img = pylibheif.HeifImage(width, height, 
                          pylibheif.HeifColorspace.RGB,
                          pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)

# Fill with data
plane = img.get_plane(pylibheif.HeifChannel.Interleaved, True)
arr = np.asarray(plane)
arr[:] = your_image_data  # your RGB data

# Encode and save
ctx = pylibheif.HeifContext()

# For HEIC (HEVC)
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.HEVC)
# For AVIF (AV1)
# encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.AV1)
# For JPEG2000
# encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.JPEG2000)

encoder.set_lossy_quality(85)
encoder.encode_image(ctx, img)
ctx.write_to_file('output.heic')

Converting HEIC to JPEG

import pylibheif
import numpy as np
from PIL import Image

# Decode HEIC
ctx = pylibheif.HeifContext()
ctx.read_from_file('input.heic')
handle = ctx.get_primary_image_handle()
img = handle.decode(pylibheif.HeifColorspace.RGB, 
                    pylibheif.HeifChroma.InterleavedRGB)

# Get NumPy array
plane = img.get_plane(pylibheif.HeifChannel.Interleaved, False)
arr = np.asarray(plane)

# Save as JPEG using PIL
pil_img = Image.fromarray(arr)
pil_img.save('output.jpg', 'JPEG', quality=85)

Reading Metadata

import pylibheif

ctx = pylibheif.HeifContext()
ctx.read_from_file('image.heic')
handle = ctx.get_primary_image_handle()

# Get metadata block IDs
exif_ids = handle.get_metadata_block_ids('Exif')
for id in exif_ids:
    metadata_type = handle.get_metadata_block_type(id)
    metadata_bytes = handle.get_metadata_block(id)
    print(f'Metadata type: {metadata_type}, size: {len(metadata_bytes)}')

API Reference

class pylibheif.HeifContext

Manages the valid lifetime of libheif context. It is the main entry point (root object) for high-level API.

Methods

__init__() Creates a new empty context.

read_from_file(filename: str) -> None Reads a HEIF file from the given filename.

  • filename: Path to the HEIF file.

read_from_memory(data: bytes) -> None Reads a HEIF file from a bytes object.

  • data: Bytes containing the file content.

write_to_file(filename: str) -> None Writes the current context to a file.

  • filename: Destination path.

write_to_bytes() -> bytes Writes the current context to a bytes object.

  • Returns: bytes object containing the encoded file data.

get_primary_image_handle() -> HeifImageHandle Gets the handle for the primary image in the file.

  • Returns: HeifImageHandle for the primary image.

get_image_handle(id: int) -> HeifImageHandle Gets the handle for a specific image ID.

  • id: The ID of the image (see get_list_of_top_level_image_IDs).
  • Returns: HeifImageHandle.

get_list_of_top_level_image_IDs() -> List[int] Gets a list of IDs of all top-level images in the file.

  • Returns: List of integer IDs.

class pylibheif.HeifImageHandle

Represents a compressed image within the HEIF file.

Properties

  • width (int): The width of the image.
  • height (int): The height of the image.
  • has_alpha (bool): True if the image has an alpha channel.

Methods

decode(colorspace: HeifColorspace = HeifColorspace.RGB, chroma: HeifChroma = HeifChroma.InterleavedRGB) -> HeifImage Decodes the image handle into an uncompressed HeifImage.

  • colorspace: Target colorspace (default: RGB).
  • chroma: Target chroma format (default: InterleavedRGB).
  • Returns: Decoded HeifImage.

get_metadata_block_ids(type_filter: str = "") -> List[str] Gets a list of metadata block IDs attached to this image.

  • type_filter: Optional filter string (e.g. "Exif", "XMP").
  • Returns: List of metadata ID strings.

get_metadata_block_type(id: str) -> str Gets the type string of a specific metadata block.

  • id: Metadata ID.
  • Returns: Type string (e.g. "Exif").

get_metadata_block(id: str) -> bytes Gets the raw data of a metadata block.

  • id: Metadata ID.
  • Returns: bytes object containing the metadata.

class pylibheif.HeifImage

Represents an uncompressed image containing pixel data. Supports the Python Buffer Protocol for zero-copy access with NumPy.

Properties

  • width (int): The width of the image.
  • height (int): The height of the image.

Methods

__init__(width: int, height: int, colorspace: HeifColorspace, chroma: HeifChroma) Creates a new empty image.

  • width: Image width.
  • height: Image height.
  • colorspace: Image colorspace.
  • chroma: Image chroma format.

add_plane(channel: HeifChannel, width: int, height: int, bit_depth: int) -> None Adds a new plane to the image.

  • channel: The channel type (e.g. HeifChannel.Interleaved).
  • width: Width of the plane.
  • height: Height of the plane.
  • bit_depth: Bit depth (e.g. 8).

get_plane(channel: HeifChannel, writeable: bool = False) -> HeifPlane Gets a plane object that supports the buffer protocol.

  • channel: The channel to retrieve.
  • writeable: Whether the buffer should be writable.
  • Returns: HeifPlane object (wrappable with np.asarray()).

class pylibheif.HeifEncoder

Controls the encoding process.

Methods

__init__(format: HeifCompressionFormat) Creates a new encoder for the specified format.

  • format: Compression format (e.g. HeifCompressionFormat.HEVC).

set_lossy_quality(quality: int) -> None Sets the quality for lossy compression.

  • quality: Integer between 0 (lowest) and 100 (highest).

set_parameter(name: str, value: str) -> None Sets a low-level encoder parameter.

  • name: Parameter name (e.g. "speed" for AV1).
  • value: Parameter value.

encode_image(context: HeifContext, image: HeifImage) -> None Encodes the given image and appends it to the context.

  • context: The destination HeifContext.
  • image: The source HeifImage to encode.

Enums

pylibheif.HeifColorspace

  • RGB, YCbCr, Monochrome, Undefined

pylibheif.HeifChroma

  • InterleavedRGB: Interleaved R, G, B bytes.
  • InterleavedRGBA: Interleaved R, G, B, A bytes.
  • C420: YUV 4:2:0 planar.
  • C422: YUV 4:2:2 planar.
  • C444: YUV 4:4:4 planar.
  • Monochrome.

pylibheif.HeifChannel

  • Interleaved: For interleaved RGB/RGBA.
  • Y, Cb, Cr: For YUV planar.
  • R, G, B: For RGB planar.
  • Alpha: For Alpha channel.

pylibheif.HeifCompressionFormat

  • HEVC: H.265 (libx265).
  • AV1: AV1 (AOM/RAV1E/SVT).
  • JPEG: JPEG.
  • JPEG2000: JPEG 2000 (OpenJPEG).

Building from Source

# Clone with submodules
git clone --recursive https://github.com/your-username/pylibheif.git
cd pylibheif

# Build
uv pip install -e .

Performance

Benchmarks on 1920x1080 RGB image (Apple Silicon):

Operation pylibheif pillow-heif Note
HEVC Decode 25 ms 25 ms ~39 FPS
HEVC Encode 279 ms 272 ms Quality 80
AV1 Encode 91 ms - Speed 50

pylibheif offers performance comparable to pillow-heif (both wrapper libheif), but exposes a lower-level C++ API for fine-grained control.

Run benchmarks yourself:

uv pip install pillow-heif pytest-benchmark
uv run pytest tests/test_benchmark.py --benchmark-only

License

This project is licensed under the LGPL-3.0 License - see the LICENSE file for details.

Acknowledgments

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

pylibheif-1.21.2.post2.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pylibheif-1.21.2.post2-cp314-cp314-manylinux_2_28_x86_64.whl (24.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

pylibheif-1.21.2.post2-cp314-cp314-macosx_14_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

pylibheif-1.21.2.post2-cp313-cp313-manylinux_2_28_x86_64.whl (24.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

pylibheif-1.21.2.post2-cp313-cp313-macosx_14_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

pylibheif-1.21.2.post2-cp312-cp312-manylinux_2_28_x86_64.whl (24.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

pylibheif-1.21.2.post2-cp312-cp312-macosx_14_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

pylibheif-1.21.2.post2-cp311-cp311-manylinux_2_28_x86_64.whl (24.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

pylibheif-1.21.2.post2-cp311-cp311-macosx_14_0_arm64.whl (6.9 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

File details

Details for the file pylibheif-1.21.2.post2.tar.gz.

File metadata

  • Download URL: pylibheif-1.21.2.post2.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pylibheif-1.21.2.post2.tar.gz
Algorithm Hash digest
SHA256 5c8e61aa699c600dee9ad2656dcd1b81f0386eecd2edd3ce6cebee6c53719829
MD5 1ed120f28c35c35618b7a3d776712191
BLAKE2b-256 5852d7dbe90a2161db653dcb301ade296078b9d127cc7801d916ee504726f259

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2.tar.gz:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f0c43e9e91ea94db2acc6eee9eb239ef3e0ec119dd6e809da9f260c2aac8c7c0
MD5 cad9a2604879ad315d5a58af856b3f01
BLAKE2b-256 d9de78a1302098848052d9eb2d55011869f9cae92f43a0be213fa6a6123ad0c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp314-cp314-manylinux_2_28_x86_64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ed21645f0e6b22a0644bcc991d7ac64d4511b413872bd095c70baaf4e3a08bc0
MD5 2c424a434a060e9768c0b04e5841f907
BLAKE2b-256 daa7d98e4eb690d0b39bef5ae354d095ac27d801e4f2dce9eff898dd1977db0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp314-cp314-macosx_14_0_arm64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 78a9f2adec89d86ff8edff339cfcd985a269277e3e02e5f8d622dafface34132
MD5 267ec0e17d4d3d5947ef7ce35712259f
BLAKE2b-256 0d2a830cc1d296fde19e9350d7006d1f12b1961bd0aa4ffd8618d17a4fdb04ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp313-cp313-manylinux_2_28_x86_64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d03675c0c880a5eb2804a53747a57a35a15146fac6d1035d5fd10d8b0e9947e5
MD5 30c35c503031639d041d345a3236b08c
BLAKE2b-256 c5b64558fe63b195ee20de74b0884fd2bdcb1d19f3fe482989caa856444c21ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp313-cp313-macosx_14_0_arm64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 46e6b25dbae3256734c16a6fd084b28b21c878c3826180b048359315a1e50b94
MD5 3f6e51a2e5ea7f96d4a104de54974837
BLAKE2b-256 5a7a62f9f61ce4fa6dbcc52bb39735ac3d2f723e9c10039b5eb37b7d85c21e13

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp312-cp312-manylinux_2_28_x86_64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 27aa49e9b15d20081b2db1928dacc613a31f9785bf39ca578361e9074dd48843
MD5 7fe4c07b621caf46a00d14108daefe4c
BLAKE2b-256 4943505ad05c82fdbcaa7f65ce67fa29c2dc5498ed9da7f9f893c993469467c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp312-cp312-macosx_14_0_arm64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5117fc9906963b772fe35ee384752da2ca958853454e8a5ad25cd49d239dafd2
MD5 dafa0c804dcff11ec0e4bec2fac8ffc6
BLAKE2b-256 84800a05a7d945fa9cb8bbafed71823808ddd56c3223bc0be3a293dc576e1664

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp311-cp311-manylinux_2_28_x86_64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pylibheif-1.21.2.post2-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2.post2-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 06a1c1251384f948cd55186d1cf9361979cd41be6db40045a77a2c8018d34778
MD5 c394b2adf8b92cbf64f963f0c4509f29
BLAKE2b-256 99ee7623737771a9110d2dffdcbd7b54eeeff616626ce97128342a388b43b909

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2.post2-cp311-cp311-macosx_14_0_arm64.whl:

Publisher: build.yml on twn39/pylibheif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page