CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pillow-heif

Python interface for libheif library providing HEIF/AVIF image processing with both standalone and Pillow plugin capabilities

Overall
score

94%

Overview
Eval results
Files

file-operations.mddocs/

File Operations

Core functionality for opening, reading, and validating HEIF/AVIF files. These functions provide the primary interface for working with HEIF/AVIF image files, supporting format detection, multi-image containers, and various processing modes.

Capabilities

Opening HEIF Files

Opens a HEIF/AVIF file for reading without automatically decoding all images. Provides lazy loading for efficient memory usage with multi-image files.

def open_heif(fp, convert_hdr_to_8bit: bool = True, bgr_mode: bool = False, **kwargs) -> HeifFile:
    """
    Open a HEIF/AVIF file for reading.
    
    Parameters:
    - fp: file path (str) or file-like object
    - convert_hdr_to_8bit: bool, convert HDR images to 8-bit (default: True)
    - bgr_mode: bool, return BGR pixel order instead of RGB (default: False)
    - **kwargs: additional options for decoding
    
    Returns:
    HeifFile: Container object with access to all images in the file
    
    Raises:
    IOError: If file cannot be opened or is not a valid HEIF/AVIF file
    """

Usage example:

import pillow_heif

# Open HEIF file with default settings
heif_file = pillow_heif.open_heif("image.heic")

# Open with HDR preservation
heif_file = pillow_heif.open_heif("hdr_image.heic", convert_hdr_to_8bit=False)

# Open for OpenCV usage (BGR order)
heif_file = pillow_heif.open_heif("image.heic", bgr_mode=True)

Reading HEIF Files

Opens and immediately decodes all images in a HEIF/AVIF file. More memory intensive but provides immediate access to all image data.

def read_heif(fp, convert_hdr_to_8bit: bool = True, bgr_mode: bool = False, **kwargs) -> HeifFile:
    """
    Open and decode all images in a HEIF/AVIF file.
    
    Parameters:
    - fp: file path (str) or file-like object
    - convert_hdr_to_8bit: bool, convert HDR images to 8-bit (default: True)
    - bgr_mode: bool, return BGR pixel order instead of RGB (default: False)
    - **kwargs: additional options for decoding
    
    Returns:
    HeifFile: Container object with all images decoded and ready for use
    
    Raises:
    IOError: If file cannot be opened or decoded
    """

Usage example:

import pillow_heif

# Read entire HEIF file into memory
heif_file = pillow_heif.read_heif("multi_image.heic")

# Access all images immediately
for i, image in enumerate(heif_file):
    print(f"Image {i}: {image.size} {image.mode}")

Format Support Detection

Checks if a file or file-like object contains supported HEIF/AVIF data. Fast format detection without full file parsing.

def is_supported(fp) -> bool:
    """
    Check if file contains supported HEIF/AVIF data.
    
    Parameters:
    - fp: file path (str) or file-like object
    
    Returns:
    bool: True if file is supported HEIF/AVIF format, False otherwise
    
    Notes:
    - Does not validate full file integrity, only checks format signature
    - Returns False for corrupted files that may have valid headers
    """

Usage example:

import pillow_heif

# Check before processing
if pillow_heif.is_supported("unknown_format.heic"):
    heif_file = pillow_heif.open_heif("unknown_format.heic")
    # Process file
else:
    print("File format not supported")

# Batch processing with format checking
import os
for filename in os.listdir("images/"):
    filepath = os.path.join("images", filename)
    if pillow_heif.is_supported(filepath):
        heif_file = pillow_heif.open_heif(filepath)
        # Process supported files

Common Parameters

File Path Handling

All functions accept either file paths as strings or file-like objects:

# File path
heif_file = pillow_heif.open_heif("/path/to/image.heic")

# File-like object
with open("image.heic", "rb") as f:
    heif_file = pillow_heif.open_heif(f)

# BytesIO object
from io import BytesIO
heif_file = pillow_heif.open_heif(BytesIO(heif_data))

HDR and Bit Depth Handling

# Preserve HDR data (10/12-bit images)
heif_file = pillow_heif.open_heif("hdr.heic", convert_hdr_to_8bit=False)
print(f"Original bit depth preserved: {heif_file.mode}")

# Convert HDR to 8-bit for compatibility
heif_file = pillow_heif.open_heif("hdr.heic", convert_hdr_to_8bit=True)
print(f"Converted to 8-bit: {heif_file.mode}")

Color Order Options

# Standard RGB order for most applications
heif_file = pillow_heif.open_heif("image.heic", bgr_mode=False)

# BGR order for OpenCV compatibility
heif_file = pillow_heif.open_heif("image.heic", bgr_mode=True)
import cv2
np_array = np.asarray(heif_file)
cv2.imwrite("output.png", np_array)  # Direct OpenCV usage

Error Handling

import pillow_heif

try:
    if pillow_heif.is_supported("image.heic"):
        heif_file = pillow_heif.open_heif("image.heic")
        # Process file
    else:
        print("Unsupported file format")
except IOError as e:
    print(f"Error opening file: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Install with Tessl CLI

npx tessl i tessl/pypi-pillow-heif

docs

encoding.md

file-operations.md

image-classes.md

index.md

metadata-utilities.md

pillow-integration.md

tile.json