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

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

# Install with uv (recommended)
uv pip install -e .

# Or with pip
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

Classes

  • HeifContext: Main context for reading/writing HEIF files
  • HeifImageHandle: Handle to a decoded image with metadata
  • HeifImage: Raw image data with buffer protocol support
  • HeifEncoder: Encoder for creating HEIF files
  • HeifPlane: Image plane with NumPy buffer protocol

Enums

  • HeifColorspace: RGB, YCbCr, Monochrome
  • HeifChroma: C420, C422, C444, InterleavedRGB, InterleavedRGBA
  • HeifChannel: Y, Cb, Cr, R, G, B, Alpha, Interleaved
  • HeifCompressionFormat: HEVC, AV1, JPEG, JPEG2000

Exceptions

  • HeifError: Base exception for all libheif errors

Building from Source

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

# Build
uv pip install -e .

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.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-cp313-cp313-manylinux_2_28_x86_64.whl (5.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.13macOS 14.0+ ARM64

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

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

pylibheif-1.21.2-cp312-cp312-macosx_26_0_arm64.whl (1.7 MB view details)

Uploaded CPython 3.12macOS 26.0+ ARM64

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

Uploaded CPython 3.12macOS 14.0+ ARM64

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

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

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

Uploaded CPython 3.11macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: pylibheif-1.21.2.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pylibheif-1.21.2.tar.gz
Algorithm Hash digest
SHA256 e57efc4f0fc6e53ca05507babf697415631ff426bd003f1f95acdb9c235b8059
MD5 f4684ddbaf9310f8b3e390af50a56d22
BLAKE2b-256 8d986499b09691b8bc418128c3d5c8969543823adbbb3475b1df2936ade4dbd9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8652e5c8309b446d51a59387fc9181c07aae58b733194779b8de31af5063db5a
MD5 a815d84c8bb459e673ea1d190d52eee2
BLAKE2b-256 7b3452e528720922859e28afa1d4a4ffb3da95d9b654108fd6ba5de72103d619

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e4e0d4f2df3df8a9ed4be0c69379c90164469fee8d89980ab9416064dd0f0102
MD5 27a8833daaeaee4f2fc4697ef5a587c3
BLAKE2b-256 cab05f991c2a514c3ee02d883d257302ca0ea07c86ce49b1b3ccfbcc3059d9d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 591e5319069113904f5a417e1463fb99ef7350b10f8c9f2990bae3d8d303992a
MD5 5e3ddd5804feaaa070e16f91ae78cd95
BLAKE2b-256 192a171c9177c01840d0608d565b0114a1a20df0259002177080cbc6a6cf649c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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-cp312-cp312-macosx_26_0_arm64.whl.

File metadata

  • Download URL: pylibheif-1.21.2-cp312-cp312-macosx_26_0_arm64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.12, macOS 26.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pylibheif-1.21.2-cp312-cp312-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 bf84c49a9c1d38fa9984410be03f7b57b92b7fbede358aebe3d8a2009109ba01
MD5 fa08a6d041b7cb29643840a6e94de635
BLAKE2b-256 9b02b25b0937475c027d2d53a5ead8ab9ba9d20ebe5a39abf478deb7c8a33091

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 4f107e35c2d475a163f34e955a6b064b8f31dfdb766f5150a31dd72578cd5a94
MD5 10c7db10c74c61a34c10168fc327c00a
BLAKE2b-256 882c69abde462dd278420e8288aa597fb0e0d5edc66493097cc9514ac131520d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c2d21ead70347c9304033ee262954193e8a50ec3a44ecb6678c6ee2ab8aeda7e
MD5 ed077aaca73373a3c8835376c2de9304
BLAKE2b-256 721de2f91bc990f1a71d08c6dabdeff1ff1503459984d4357eae0c23886271d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for pylibheif-1.21.2-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 590b209d19cbfa5a8a2bd58badca0e973ff2df044a6d1d1559d22583b5826a71
MD5 e6afc1bbab830ed6d4c95f254e60c048
BLAKE2b-256 1a212a73797cdee441d0e5060d2e965a603856199853e53c359caddcf57cb345

See more details on using hashes here.

Provenance

The following attestation bundles were made for pylibheif-1.21.2-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