Pillow-HEIF

1.3.0 · active · verified Fri Apr 10

Pillow-HEIF (pi-heif) provides a robust Python interface for the libheif library, enabling reading and writing of HEIF/HEIC image files. It integrates seamlessly with the popular Pillow imaging library, extending its capabilities to support the HEIF format. The library is actively maintained, with version 1.3.0 being the latest, and releases are frequent.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register `pillow_heif` with Pillow, create a dummy image, save it in HEIF/HEIC format, and then open and convert it to JPEG. This showcases the library's ability to extend Pillow's image handling.

import os
from PIL import Image
from pillow_heif import register_heif_opener

# Register HEIF/HEIC support with Pillow
register_heif_opener()

dummy_heic_path = "dummy.heic"
output_jpeg_path = "output.jpg"

try:
    # Create a dummy image using Pillow
    img = Image.new("RGB", (200, 150), color='blue')
    print(f"Created a dummy Pillow image of size {img.size}")

    # Save the dummy image as HEIF/HEIC using the registered opener
    # Note: HEIF encoding might require libheif to be compiled with specific encoders
    # If save fails, ensure libheif has an encoder available.
    img.save(dummy_heic_path, format="HEIF", quality=80)
    print(f"Saved dummy image to {dummy_heic_path} as HEIF.")

    # Open the HEIF/HEIC file using Pillow's Image.open()
    with Image.open(dummy_heic_path) as heic_img:
        print(f"Successfully opened HEIC image: Format={heic_img.format}, Mode={heic_img.mode}, Size={heic_img.size}")
        
        # Save it as JPEG
        heic_img.save(output_jpeg_path)
        print(f"Saved HEIC image to {output_jpeg_path} as JPEG.")

except Exception as e:
    print(f"An error occurred during quickstart: {e}")
    print("This might be due to missing underlying libheif encoders/decoders, or unsupported system configuration.")
finally:
    # Clean up created files
    if os.path.exists(dummy_heic_path):
        os.remove(dummy_heic_path)
        print(f"Cleaned up {dummy_heic_path}")
    if os.path.exists(output_jpeg_path):
        os.remove(output_jpeg_path)
        print(f"Cleaned up {output_jpeg_path}")

view raw JSON →