Skip to main content

pylibheif

PyPI version Build and Publish License: LGPL v3 Python 3.11+

Python bindings for libheif using nanobind.

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 and write EXIF, XMP, and custom metadata
  • HDR Metadata Support: Read and write HDR metadata (CLLI, MDCV, AMVE) using physical units (Nits, Lux, CIE coordinates) with type safety
  • Asynchronous Support: Built-in asyncio wrappers for non-blocking I/O and encoding
  • RAII Resource Management: Automatic resource cleanup with context managers

Supported Formats

Format Decoding Encoding Codec
HEIC (HEVC/H.265) ✅ ✅ libde265 (Dec) / x265 (Enc) / Kvazaar (Enc)
AVIF (AV1) ✅ ✅ DAV1D (Dec) / AOM (Enc)
JPEG2000 ✅ ✅ OpenJPEG
JPEG ✅ ✅ libjpeg

Requirements

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

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)
    arr = img.get_plane(pylibheif.HeifChannel.Interleaved, False)  # 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)
arr = img.get_plane(pylibheif.HeifChannel.Interleaved, False)

# Resources are automatically freed when objects go out of scope

Writing HEIC Images (H.265)

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
arr = img.get_plane(pylibheif.HeifChannel.Interleaved, True)
arr[:] = your_image_data  # your RGB data

# Encode and save as HEIC
ctx = pylibheif.HeifContext()
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.HEVC)
encoder.set_lossy_quality(85)
encoder.encode_image(ctx, img)

ctx.write_to_file('output.heic')

Writing AVIF Images (AV1)

import pylibheif
import numpy as np

# Prepare image (same as above)
width, height = 1920, 1080
img = pylibheif.HeifImage(width, height, 
                          pylibheif.HeifColorspace.RGB,
                          pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)

# Encode and save as AVIF
ctx = pylibheif.HeifContext()

# Use AV1 format for AVIF
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.AV1)
encoder.set_lossy_quality(85)
encoder.set_parameter("speed", "6") # Optional: Tune speed (0-9)

encoder.encode_image(ctx, img)

# Save with .avif extension
ctx.write_to_file('output.avif')

Writing JPEG Images

import pylibheif
import numpy as np

# Prepare image (same as above)
width, height = 1920, 1080
img = pylibheif.HeifImage(width, height, 
                          pylibheif.HeifColorspace.RGB,
                          pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)

# Encode and save as JPEG
ctx = pylibheif.HeifContext()

# Use JPEG format
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.JPEG)
encoder.set_lossy_quality(90) # Quality 0-100

encoder.encode_image(ctx, img)

# Save with .jpg extension
ctx.write_to_file('output.jpg')

Writing JPEG2000 Images

import pylibheif
import numpy as np

# Prepare image (same as above)
width, height = 1920, 1080
img = pylibheif.HeifImage(width, height, 
                          pylibheif.HeifColorspace.RGB,
                          pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)

# Encode and save as JPEG2000 in HEIF container
ctx = pylibheif.HeifContext()

# Use JPEG2000 format
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.JPEG2000)
encoder.set_lossy_quality(85) # Quality 0-100

encoder.encode_image(ctx, img)

# Save with .jp2 or .heif extension
# Note: libheif typically saves JPEG2000 in a HEIF container
ctx.write_to_file('output.heif')

Encoder Selection

By default, pylibheif selects the best available encoder for the requested format (e.g. x265 for HEVC). You can also explicitly select a specific encoder (e.g. Kvazaar) if available.

import pylibheif

# 1. Get all available HEVC encoders
descriptors = pylibheif.get_encoder_descriptors(pylibheif.HeifCompressionFormat.HEVC)

# Print available encoders
for d in descriptors:
    print(f"ID: {d.id_name}, Name: {d.name}")

# 2. Find specific encoder (e.g. Kvazaar)
kvazaar_desc = next((d for d in descriptors if "kvazaar" in d.id_name), None)

if kvazaar_desc:
    # 3. Create encoder explicitly using the descriptor
    encoder = pylibheif.HeifEncoder(kvazaar_desc)
    
    # Verify which encoder is used
    print(f"Using encoder: {encoder.name}")
    
    encoder.set_lossy_quality(85)
    # encoder.encode_image(...)
    # encoder.encode_image(...)

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)}')

Writing Metadata

import pylibheif
import numpy as np

# Create and encode an image
width, height = 64, 64
img = pylibheif.HeifImage(width, height,
                          pylibheif.HeifColorspace.RGB,
                          pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)
arr = img.get_plane(pylibheif.HeifChannel.Interleaved, True)
arr[:] = 128  # fill with gray

ctx = pylibheif.HeifContext()
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.HEVC)
encoder.set_lossy_quality(85)
handle = encoder.encode_image(ctx, img)

# Add EXIF metadata (with 4-byte offset prefix for TIFF header)
exif_data = b'\x00\x00\x00\x00' + b'Exif\x00\x00' + b'II*\x00...'  # your EXIF data
ctx.add_exif_metadata(handle, exif_data)

# Add XMP metadata
xmp_data = b'''<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/">
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
      <dc:creator>My App</dc:creator>
    </rdf:Description>
  </rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>'''
ctx.add_xmp_metadata(handle, xmp_data)

# Add custom/generic metadata
custom_data = b'{"app": "myapp", "version": "1.0"}'
ctx.add_generic_metadata(handle, custom_data, "json", "application/json")

# Save
ctx.write_to_file('output_with_metadata.heic')

HDR Metadata

pylibheif supports reading and writing standard HDR metadata introduced in libheif 1.23.0:

  • Content Light Level (CLLI): MaxCLL (Max Content Light Level) and MaxFALL (Max Frame Average Light Level) in nits.
  • Mastering Display Colour Volume (MDCV): Mastering display primaries (Red, Green, Blue, White point) in CIE xy coordinates, and luminance range in nits.
  • Ambient Viewing Environment (AMVE): Ambient illumination in lux, and ambient light CIE xy coordinates.

The API exposes these values using standard float coordinates and physical units. It automatically handles the underlying fixed-point scaling and Annex E array index mapping (e.g. green-blue-red ordering in MDCV).

Reading HDR Metadata

You can query and retrieve HDR metadata from HeifImageHandle (or AsyncHeifImageHandle):

import pylibheif

with pylibheif.HeifContext() as ctx:
    ctx.read_from_file('hdr_image.heic')
    handle = ctx.get_primary_image_handle()
    
    # 1. Content Light Level (CLLI)
    if handle.has_content_light_level:
        cll = handle.content_light_level
        print(f"MaxCLL: {cll.max_content_light_level} nits")
        print(f"MaxFALL: {cll.max_pic_average_light_level} nits")
        
    # 2. Mastering Display Colour Volume (MDCV)
    if handle.has_mastering_display_colour_volume:
        mdcv = handle.mastering_display_colour_volume
        print(f"Red Primary: {mdcv.red_primary}")        # e.g., (0.680, 0.320)
        print(f"Green Primary: {mdcv.green_primary}")    # e.g., (0.265, 0.690)
        print(f"Blue Primary: {mdcv.blue_primary}")      # e.g., (0.150, 0.060)
        print(f"White Point: {mdcv.white_point}")        # e.g., (0.3127, 0.3290)
        print(f"Luminance Range: {mdcv.min_luminance} to {mdcv.max_luminance} nits")
        
    # 3. Ambient Viewing Environment (AMVE)
    if handle.has_ambient_viewing_environment:
        amve = handle.ambient_viewing_environment
        print(f"Ambient Illumination: {amve.ambient_illumination} lux")
        print(f"Ambient Light: {amve.ambient_light}")

Writing HDR Metadata

You can attach HDR metadata to HeifImage prior to encoding:

import pylibheif
import numpy as np

# Prepare image
img = pylibheif.HeifImage(64, 64, pylibheif.HeifColorspace.RGB, pylibheif.HeifChroma.InterleavedRGB)
img.add_plane(pylibheif.HeifChannel.Interleaved, 64, 64, 8)
arr = img.get_plane(pylibheif.HeifChannel.Interleaved, True)
arr[:] = 128  # Fill with gray

# 1. Set CLLI
img.content_light_level = pylibheif.HeifContentLightLevel(
    max_content_light_level=1000,
    max_pic_average_light_level=400
)

# 2. Set MDCV (mastering display characteristics)
img.mastering_display_colour_volume = pylibheif.HeifMasteringDisplayColourVolume(
    red_primary=(0.680, 0.320),
    green_primary=(0.265, 0.690),
    blue_primary=(0.150, 0.060),
    white_point=(0.3127, 0.3290),
    max_luminance=1000.0,
    min_luminance=0.005
)

# 3. Set AMVE
img.ambient_viewing_environment = pylibheif.HeifAmbientViewingEnvironment(
    ambient_illumination=315.5,
    ambient_light=(0.3127, 0.3290)
)

# Encode
ctx = pylibheif.HeifContext()
encoder = pylibheif.HeifEncoder(pylibheif.HeifCompressionFormat.HEVC)
encoder.encode_image(ctx, img)
ctx.write_to_file('hdr_output.heic')

Asynchronous Support (asyncio)

pylibheif provides asynchronous wrappers for non-blocking I/O and CPU-intensive operations (like encoding and decoding) using asyncio.to_thread.

Async Reading and Decoding

import pylibheif
import asyncio
import numpy as np

async def read_async():
    # Recommended: Use 'async with' context manager
    async with pylibheif.AsyncHeifContext() as ctx:
        await ctx.read_from_file('image.heic')
        
        handle = ctx.get_primary_image_handle()
        
        # Asynchronously decode (offloaded to thread)
        img = await handle.decode(pylibheif.HeifColorspace.RGB, 
                                  pylibheif.HeifChroma.InterleavedRGB)
        
        arr = img.get_plane(pylibheif.HeifChannel.Interleaved, False)
        return arr

asyncio.run(read_async())

Async Encoding and Writing

import pylibheif
import asyncio
import numpy as np

async def write_async(image_data):
    width, height = 1920, 1080
    img = pylibheif.HeifImage(width, height, 
                              pylibheif.HeifColorspace.RGB,
                              pylibheif.HeifChroma.InterleavedRGB)
    img.add_plane(pylibheif.HeifChannel.Interleaved, width, height, 8)
    arr = img.get_plane(pylibheif.HeifChannel.Interleaved, True)
    arr[:] = image_data

    async with pylibheif.AsyncHeifContext() as ctx:
        encoder = pylibheif.AsyncHeifEncoder(pylibheif.HeifCompressionFormat.HEVC)
        encoder.set_lossy_quality(85)
        
        # Asynchronously encode (offloaded to thread)
        await encoder.encode_image(ctx, img)
        
        # Asynchronously write to file
        await ctx.write_to_file('output.heic')

asyncio.run(write_async(your_image_data))

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.

add_exif_metadata(handle: HeifImageHandle, data: bytes) -> None Adds EXIF metadata to the specified image.

  • handle: Image handle from encoding.
  • data: Raw EXIF bytes (with 4-byte offset prefix for TIFF header).

add_xmp_metadata(handle: HeifImageHandle, data: bytes) -> None Adds XMP metadata to the specified image.

  • handle: Image handle from encoding.
  • data: XMP XML as bytes.

add_generic_metadata(handle: HeifImageHandle, data: bytes, item_type: str, content_type: str = "") -> None Adds generic/custom metadata to the specified image.

  • handle: Image handle from encoding.
  • data: Raw metadata bytes.
  • item_type: Metadata item type (e.g. "json", "iptc").
  • content_type: Optional MIME content type (e.g. "application/json").

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.
  • has_content_light_level (bool): True if CLLI metadata is present.
  • has_mastering_display_colour_volume (bool): True if MDCV metadata is present.
  • has_ambient_viewing_environment (bool): True if AMVE metadata is present.
  • content_light_level (Optional[HeifContentLightLevel]): CLLI metadata if present, otherwise None.
  • mastering_display_colour_volume (Optional[HeifMasteringDisplayColourVolume]): MDCV metadata if present, otherwise None.
  • ambient_viewing_environment (Optional[HeifAmbientViewingEnvironment]): AMVE metadata if present, otherwise None.

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 zero-copy memory access via nanobind's ndarray integration.

Properties

  • width (int): The width of the image.
  • height (int): The height of the image.
  • has_content_light_level (bool): True if CLLI metadata is present.
  • has_mastering_display_colour_volume (bool): True if MDCV metadata is present.
  • has_ambient_viewing_environment (bool): True if AMVE metadata is present.
  • content_light_level (Optional[HeifContentLightLevel]): Get or set CLLI metadata.
  • mastering_display_colour_volume (Optional[HeifMasteringDisplayColourVolume]): Get or set MDCV metadata.
  • ambient_viewing_environment (Optional[HeifAmbientViewingEnvironment]): Get or set AMVE metadata.

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) -> np.ndarray Gets a zero-copy NumPy array mapping to the image plane memory.

  • channel: The channel to retrieve.
  • writeable: Whether the buffer should be writable.
  • Returns: numpy.ndarray mapped to the underlying libheif memory.

class pylibheif.HeifEncoder

Controls the encoding process.

Properties

  • parameters (HeifEncoderParametersProxy): A dictionary-like interface providing access to all configurable encoder parameters. It dynamically routes settings to the correct type-safe setters with validation.

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_lossless(lossless: bool) -> None Enables or disables lossless compression.

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

  • name: Parameter name.
  • value: Parameter value.

get_parameter(name: str) -> str Gets the string representation of a parameter value.

set_integer_parameter(name: str, value: int) -> None get_integer_parameter(name: str) -> int set_boolean_parameter(name: str, value: bool) -> None get_boolean_parameter(name: str) -> bool set_string_parameter(name: str, value: str) -> None get_string_parameter(name: str) -> str Type-safe getters and setters for parameter values.

encode_image(context: HeifContext, image: HeifImage, preset: str = "") -> HeifImageHandle Encodes the given image and appends it to the context.

  • context: The destination HeifContext.
  • image: The source HeifImage to encode.
  • preset: Optional encoder preset (e.g. "ultrafast", "slow"). Default is empty (balanced/default). Note: This maps to the 'preset' parameter in libheif. It works for x265 (check version), but AOM and others may use different parameters (e.g. 'speed') which should be set via set_parameter or parameters instead.
  • Returns: HeifImageHandle for the encoded image. Can be used to add metadata.

Encoder Parameters Introspection

pylibheif supports inspecting and dynamically validating encoder parameters at runtime using encoder.parameters.

class pylibheif.HeifEncoderParameter

Describes a parameter supported by the selected encoder.

  • name (str): Parameter name.
  • type (HeifEncoderParameterType): Data type of the parameter (Integer, Boolean, or String).
  • has_default (bool): Whether the parameter has a default value.
  • default_value (Union[int, bool, str, None]): The default value.
  • valid_integer_range (Optional[Tuple[int, int]]): Min and max allowed integers if bounded.
  • valid_integer_values (Optional[List[int]]): Specific allowed integers.
  • valid_string_values (Optional[List[str]]): Specific allowed string choices.

enum pylibheif.HeifEncoderParameterType

  • Integer
  • Boolean
  • String

HDR Metadata Classes

class pylibheif.HeifContentLightLevel

Holds Content Light Level Information (CLLI) as integers.

  • max_content_light_level (int): Maximum content light level (MaxCLL) in nits.
  • max_pic_average_light_level (int): Maximum picture average light level (MaxFALL) in nits.

class pylibheif.HeifMasteringDisplayColourVolume

Holds Mastering Display Colour Volume (MDCV) metadata. All coordinates are normalized CIE 1931 xy floating-point coordinates.

  • red_primary (Tuple[float, float]): Chromaticity coordinates (x, y) of the red primary.
  • green_primary (Tuple[float, float]): Chromaticity coordinates (x, y) of the green primary.
  • blue_primary (Tuple[float, float]): Chromaticity coordinates (x, y) of the blue primary.
  • white_point (Tuple[float, float]): Chromaticity coordinates (x, y) of the white point.
  • max_luminance (float): Maximum display mastering luminance in nits.
  • min_luminance (float): Minimum display mastering luminance in nits.

class pylibheif.HeifAmbientViewingEnvironment

Holds Ambient Viewing Environment (AMVE) metadata.

  • ambient_illumination (float): Ambient illumination in lux.
  • ambient_light (Tuple[float, float]): Chromaticity coordinates (x, y) of the ambient light.

class pylibheif.AsyncHeifContext

Asynchronous wrapper for HeifContext. Methods are awaited and offloaded to a background thread.

Methods

async read_from_file(filename: str) -> None async read_from_memory(data: bytes) -> None async write_to_file(filename: str) -> None async write_to_bytes() -> bytes get_primary_image_handle() -> AsyncHeifImageHandle get_image_handle(id: int) -> AsyncHeifImageHandle


class pylibheif.AsyncHeifImageHandle

Asynchronous wrapper for HeifImageHandle.

Methods

async decode(colorspace, chroma) -> HeifImage Asynchronously decodes the image.


class pylibheif.AsyncHeifEncoder

Asynchronous wrapper for HeifEncoder.

Properties

  • parameters (HeifEncoderParametersProxy): Synchronous dictionary-like proxy to access and set encoder parameters.

Methods

async encode_image(ctx, image, preset="") -> HeifImageHandle Asynchronously encodes the image.


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 (HD) RGB real-world images (Apple Silicon), comparing python libraries.

Operation Library / Encoder Mean Time
Decoding pillow-heif ~60.5 ms
Decoding pylibheif (HEVC) ~71.3 ms
Encoding pylibheif / Kvazaar ~174.4 ms
Encoding pillow-heif / x265 ~247.3 ms
Encoding pylibheif / x265 (Q80) ~255.7 ms
Encoding pylibheif / AV1 (AOM) ~292.4 ms

Key Findings:

  1. Decoding Parity: pylibheif natively fetching Python 0-copy numpy arrays from underlying native libheif decoders delivers heavily competitive and fast decode speeds within 10ms of specially tuned PIL alternatives.
  2. HEVC Performance: The kvazaar encoder significantly outperforms the default fallback x265 options available in other packages while retaining identical interfaces.
  3. Versatility: pylibheif brings near-realtime AV1 encoding capabilities reliably into Python ecosystem bounds without falling apart gracefully scaling AV1 complexity.
Raw Benchmark Output (Apple M Series)
-----------------------------------------------------------------------------------------------------------------------------------
Name (time in ms)                          Mean            OPS
-----------------------------------------------------------------------------------------------------------------------------------
test_benchmark_decode_hevc_pillow         60.56          16.49  (pillow-heif)
test_benchmark_decode_hevc                71.34          14.01  (pylibheif direct)
test_benchmark_encode_kvazaar            174.47           5.73  (pylibheif, kvazaar, Q80)
test_benchmark_encode_hevc_pillow        247.33           4.04  (pillow-heif, x265, Q80)
test_benchmark_encode_hevc               255.72           3.91  (pylibheif, x265, Q80)
test_benchmark_encode_av1                292.44           3.41  (pylibheif, aom, speed=6)
-----------------------------------------------------------------------------------------------------------------------------------

Run benchmarks yourself:

uv pip install pillow-heif pytest-benchmark
uv run pytest tests/test_benchmark.py --benchmark-only --benchmark-min-rounds=20

License

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

Acknowledgments

Metadata

Release files for pylibheif 1.23.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylibheif 1.23.1
File Size Uploaded
pylibheif-1.23.1.tar.gz 3.6 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for pylibheif 1.23.1
File
pylibheif-1.23.1-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
pylibheif-1.23.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details
pylibheif-1.23.1-cp314-cp314-macosx_14_0_arm64.whl CPython 3.14 CPython 3.14 macOS 14.0+ ARM64 Details
pylibheif-1.23.1-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
pylibheif-1.23.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
pylibheif-1.23.1-cp313-cp313-macosx_14_0_arm64.whl CPython 3.13 CPython 3.13 macOS 14.0+ ARM64 Details
pylibheif-1.23.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
pylibheif-1.23.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
pylibheif-1.23.1-cp312-cp312-macosx_14_0_arm64.whl CPython 3.12 CPython 3.12 macOS 14.0+ ARM64 Details
pylibheif-1.23.1-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
pylibheif-1.23.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details
pylibheif-1.23.1-cp311-cp311-macosx_14_0_arm64.whl CPython 3.11 CPython 3.11 macOS 14.0+ ARM64 Details

Total release size: 75.8 MB

Release files / pylibheif-1.23.1.tar.gz

Download URL pylibheif-1.23.1.tar.gz
Size 3.6 MB
Tags Source
SHA-256 checksum
How to use checksums
1bf025ec1f6cc3fec634b85189045c98668a07c231fb117b53cf2062f8bc5894
BLAKE2b-256 checksum
How to use checksums
52d1431a5c1ab03156de92fc1b94e1ae029da1f09bd8dc94639f6129f5a81068
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp314-cp314-win_amd64.whl

Download URL pylibheif-1.23.1-cp314-cp314-win_amd64.whl
Size 6.7 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
1b62f3ae4f12c47cc0a16a7162aba7d53c0085959b2a8942216ebb384754f0cb
BLAKE2b-256 checksum
How to use checksums
55ee4da8e5ad490895c5d3e7a08a88dd7a0867e1403575d9b85fbeaf22da32bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL pylibheif-1.23.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 5.8 MB
Tags CPython 3.14 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
c497e13ab1c8f899ae19e2f41f14639ee63b0b8d4ceb6e9653d415b8e4b7e361
BLAKE2b-256 checksum
How to use checksums
8fee581b9a5ba8acb03339a68102912836dcf0442e2845e75e0dc46f802bc60a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp314-cp314-macosx_14_0_arm64.whl

Download URL pylibheif-1.23.1-cp314-cp314-macosx_14_0_arm64.whl
Size 5.6 MB
Tags CPython 3.14 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
90fe24eca770fe445df9848d12d6defa367db667bf50ba55961c76c0083eb0b0
BLAKE2b-256 checksum
How to use checksums
234ed2d4e6a67a5b3cb0149dea362264ea5cf6e57dfded89dc323d1c19ae9341
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp313-cp313-win_amd64.whl

Download URL pylibheif-1.23.1-cp313-cp313-win_amd64.whl
Size 6.6 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
9a13c6110eacbf3affafe492cb94829617d60f3133c8aaaaefeeec73c95815ff
BLAKE2b-256 checksum
How to use checksums
2a712b9fa2d9fa10785c034c0b23beeeeb1cd7a1623e5622fae7cea3256e353e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL pylibheif-1.23.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 5.8 MB
Tags CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
0675ef82fea0c288208fbf7a29a6cd5dca081f47dfddc94e2d539bfdfe44c2c0
BLAKE2b-256 checksum
How to use checksums
3cf04162f2eafe2e645d47f8dd03a072954a481d36ed6102aa93aff856d145e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp313-cp313-macosx_14_0_arm64.whl

Download URL pylibheif-1.23.1-cp313-cp313-macosx_14_0_arm64.whl
Size 5.6 MB
Tags CPython 3.13 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
f000dea3d636dfa31545f20df4f39e142df9a571e42844bca0ecc176756744e4
BLAKE2b-256 checksum
How to use checksums
bf8d3b3686d1974d25b31b357639c7e0dc77379101b95f9661b168e57e376056
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp312-cp312-win_amd64.whl

Download URL pylibheif-1.23.1-cp312-cp312-win_amd64.whl
Size 6.6 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
493400e79bb7fdd4c6f2685c002adda98af71eb39c6b81c5be6e01fd64e38bce
BLAKE2b-256 checksum
How to use checksums
fa84802513b57af099c25e9d87848d878e1fe70fa1d68536942b56e03942f45e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL pylibheif-1.23.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 5.8 MB
Tags CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
ded794fe641fbf5ad606c0904185edbb4bd8aa1aab7227f1d085a2e1eba60950
BLAKE2b-256 checksum
How to use checksums
400aca9399cb270e5bc6e2608db0921b3ba68b8f93f030ffe4db8fdd52482ae5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp312-cp312-macosx_14_0_arm64.whl

Download URL pylibheif-1.23.1-cp312-cp312-macosx_14_0_arm64.whl
Size 5.6 MB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
eefeb9f1a15634a818cee14100ddb232a5cacbc8bb27c4c10651512b08f3350d
BLAKE2b-256 checksum
How to use checksums
6b6f2a8c057c57568e26d5c24e344e40f004a825c27c8eceba38b1171e40fd44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp311-cp311-win_amd64.whl

Download URL pylibheif-1.23.1-cp311-cp311-win_amd64.whl
Size 6.6 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
bc6ee23e147a06e4566ac4fd7c108cea9ad1aa59fe8cc0289b6da2222e1b8991
BLAKE2b-256 checksum
How to use checksums
3109027aaca3dfbef8d7372c9b9f9dcb952f79b1a0a337dfceb12f9206062d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL pylibheif-1.23.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 5.8 MB
Tags CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
45d08b3a88b68a00936d0ed5b969f99d5b58c82de6c2615a86526aafb401ec57
BLAKE2b-256 checksum
How to use checksums
49135a84cfb714c5b47e2d78851db35ca72697e4f271fef48bc6c299b7ecfb88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log

Release files / pylibheif-1.23.1-cp311-cp311-macosx_14_0_arm64.whl

Download URL pylibheif-1.23.1-cp311-cp311-macosx_14_0_arm64.whl
Size 5.6 MB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
9ac7ec090380eb884c133b89d3b3aedcf9f931ff83fb44f1ed0b6420e3fa98c5
BLAKE2b-256 checksum
How to use checksums
5bfc12b2fc84c60d39123f2b7d7af5e4dbc0a293936ea4c4995db3cf31f46d0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 18, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page