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 filesHeifImageHandle: Handle to a decoded image with metadataHeifImage: Raw image data with buffer protocol supportHeifEncoder: Encoder for creating HEIF filesHeifPlane: Image plane with NumPy buffer protocol
Enums
HeifColorspace: RGB, YCbCr, MonochromeHeifChroma: C420, C422, C444, InterleavedRGB, InterleavedRGBAHeifChannel: Y, Cb, Cr, R, G, B, Alpha, InterleavedHeifCompressionFormat: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e57efc4f0fc6e53ca05507babf697415631ff426bd003f1f95acdb9c235b8059
|
|
| MD5 |
f4684ddbaf9310f8b3e390af50a56d22
|
|
| BLAKE2b-256 |
8d986499b09691b8bc418128c3d5c8969543823adbbb3475b1df2936ade4dbd9
|
File details
Details for the file pylibheif-1.21.2-cp313-cp313-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp313-cp313-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.9 MB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8652e5c8309b446d51a59387fc9181c07aae58b733194779b8de31af5063db5a
|
|
| MD5 |
a815d84c8bb459e673ea1d190d52eee2
|
|
| BLAKE2b-256 |
7b3452e528720922859e28afa1d4a4ffb3da95d9b654108fd6ba5de72103d619
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp313-cp313-manylinux_2_28_x86_64.whl -
Subject digest:
8652e5c8309b446d51a59387fc9181c07aae58b733194779b8de31af5063db5a - Sigstore transparency entry: 900619699
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pylibheif-1.21.2-cp313-cp313-macosx_14_0_arm64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp313-cp313-macosx_14_0_arm64.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.13, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4e0d4f2df3df8a9ed4be0c69379c90164469fee8d89980ab9416064dd0f0102
|
|
| MD5 |
27a8833daaeaee4f2fc4697ef5a587c3
|
|
| BLAKE2b-256 |
cab05f991c2a514c3ee02d883d257302ca0ea07c86ce49b1b3ccfbcc3059d9d7
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp313-cp313-macosx_14_0_arm64.whl -
Subject digest:
e4e0d4f2df3df8a9ed4be0c69379c90164469fee8d89980ab9416064dd0f0102 - Sigstore transparency entry: 900619754
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pylibheif-1.21.2-cp312-cp312-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp312-cp312-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.9 MB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
591e5319069113904f5a417e1463fb99ef7350b10f8c9f2990bae3d8d303992a
|
|
| MD5 |
5e3ddd5804feaaa070e16f91ae78cd95
|
|
| BLAKE2b-256 |
192a171c9177c01840d0608d565b0114a1a20df0259002177080cbc6a6cf649c
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp312-cp312-manylinux_2_28_x86_64.whl -
Subject digest:
591e5319069113904f5a417e1463fb99ef7350b10f8c9f2990bae3d8d303992a - Sigstore transparency entry: 900619631
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf84c49a9c1d38fa9984410be03f7b57b92b7fbede358aebe3d8a2009109ba01
|
|
| MD5 |
fa08a6d041b7cb29643840a6e94de635
|
|
| BLAKE2b-256 |
9b02b25b0937475c027d2d53a5ead8ab9ba9d20ebe5a39abf478deb7c8a33091
|
File details
Details for the file pylibheif-1.21.2-cp312-cp312-macosx_14_0_arm64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp312-cp312-macosx_14_0_arm64.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.12, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f107e35c2d475a163f34e955a6b064b8f31dfdb766f5150a31dd72578cd5a94
|
|
| MD5 |
10c7db10c74c61a34c10168fc327c00a
|
|
| BLAKE2b-256 |
882c69abde462dd278420e8288aa597fb0e0d5edc66493097cc9514ac131520d
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp312-cp312-macosx_14_0_arm64.whl -
Subject digest:
4f107e35c2d475a163f34e955a6b064b8f31dfdb766f5150a31dd72578cd5a94 - Sigstore transparency entry: 900619886
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pylibheif-1.21.2-cp311-cp311-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp311-cp311-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 5.9 MB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2d21ead70347c9304033ee262954193e8a50ec3a44ecb6678c6ee2ab8aeda7e
|
|
| MD5 |
ed077aaca73373a3c8835376c2de9304
|
|
| BLAKE2b-256 |
721de2f91bc990f1a71d08c6dabdeff1ff1503459984d4357eae0c23886271d1
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp311-cp311-manylinux_2_28_x86_64.whl -
Subject digest:
c2d21ead70347c9304033ee262954193e8a50ec3a44ecb6678c6ee2ab8aeda7e - Sigstore transparency entry: 900619953
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pylibheif-1.21.2-cp311-cp311-macosx_14_0_arm64.whl.
File metadata
- Download URL: pylibheif-1.21.2-cp311-cp311-macosx_14_0_arm64.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.11, macOS 14.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
590b209d19cbfa5a8a2bd58badca0e973ff2df044a6d1d1559d22583b5826a71
|
|
| MD5 |
e6afc1bbab830ed6d4c95f254e60c048
|
|
| BLAKE2b-256 |
1a212a73797cdee441d0e5060d2e965a603856199853e53c359caddcf57cb345
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pylibheif-1.21.2-cp311-cp311-macosx_14_0_arm64.whl -
Subject digest:
590b209d19cbfa5a8a2bd58badca0e973ff2df044a6d1d1559d22583b5826a71 - Sigstore transparency entry: 900619826
- Sigstore integration time:
-
Permalink:
twn39/pylibheif@cc48a0640b581605c2778b80fbe7278a713f0448 -
Branch / Tag:
refs/tags/v1.21.2 - Owner: https://github.com/twn39
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@cc48a0640b581605c2778b80fbe7278a713f0448 -
Trigger Event:
push
-
Statement type: