Pillow-HEIF

1.3.0 · active · verified Thu Apr 09

Pillow-HEIF is a Python interface for the libheif library, enabling the Pillow (PIL Fork) imaging library to open and save HEIF (High Efficiency Image File Format) and HEIC images. The current version is 1.3.0 and the project maintains an active release cadence with frequent updates to bundled libraries and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register HEIF support with Pillow, open an existing HEIF/HEIC image, convert it to JPEG, and then save an image back into the HEIC format. Ensure you have an 'input.heif' file in your working directory or provide a path via `HEIF_INPUT_PATH` environment variable. The `os.environ.get` is used to make it runnable without hardcoding paths.

from PIL import Image
from pillow_heif import register_heif_opener
import os

# Register the HEIF opener once at the start of your application
register_heif_opener()

# --- Example: Open and convert a HEIF/HEIC image ---
heif_path = os.environ.get('HEIF_INPUT_PATH', 'input.heif')
output_path = os.environ.get('OUTPUT_JPEG_PATH', 'output.jpeg')

try:
    # Open a HEIF image
    heif_img = Image.open(heif_path)
    print(f"Successfully opened HEIF image: {heif_path}")
    print(f"Image format: {heif_img.format}, size: {heif_img.size}, mode: {heif_img.mode}")

    # Convert and save to JPEG
    # Note: HEIF often has 10-bit or 12-bit depth. For 8-bit formats like JPEG,
    # Pillow will convert, potentially losing some detail.
    if heif_img.mode == 'LA': # Handle monochrome with alpha
        heif_img = heif_img.convert('L')
    elif 'A' in heif_img.mode: # Remove alpha for JPEG
        heif_img = heif_img.convert('RGB')
    else:
        heif_img = heif_img.convert('RGB') # Ensure RGB for JPEG

    heif_img.save(output_path, quality=90)
    print(f"Saved converted image to: {output_path}")

except FileNotFoundError:
    print(f"Error: HEIF input file '{heif_path}' not found.")
    print("Please ensure 'input.heif' exists or set HEIF_INPUT_PATH environment variable.")
except Exception as e:
    print(f"An error occurred: {e}")

# --- Example: Create and save a HEIF/HEIC image (requires an input image first) ---
# This part assumes 'output.jpeg' was created or exists.
if os.path.exists(output_path):
    try:
        jpeg_img = Image.open(output_path)
        heic_output_path = os.environ.get('OUTPUT_HEIC_PATH', 'generated.heic')
        # Save as HEIC. You can specify encoder parameters in .info['heif_metadata']
        jpeg_img.save(heic_output_path, quality=80, save_all=True)
        print(f"Saved image back to HEIC: {heic_output_path}")
    except Exception as e:
        print(f"An error occurred while saving to HEIC: {e}")
else:
    print(f"Skipping HEIC creation: '{output_path}' not found.")

view raw JSON →