Skip to content

Working with heic images on Python

Pillow / PIL can't open heic images by default. There are few options to open and working with heic images. Seems like, there are some license constrains to use ship with the hardware. Need to have a mechanism to download separately. (TODO: understand impact)

pillow-heif

This enables heic capability to pillow.

pip3 install pillow-heif
from PIL import Image
from pillow_heif import register_heif_opener

register_heif_opener()

image = Image.open('image.heic')

#To save
image.save(filepath, format="jpg", ...)`
Found from here. This comes with BSD-3-Clause license. This is an Python bindings to libheif for working with HEIF images.

pyheif

pip install whatimage
pip install pyheif
    import io

     import whatimage
     import pyheif
     from PIL import Image


     def decodeImage(bytesIo):

        fmt = whatimage.identify_image(bytesIo)
        if fmt in ['heic', 'avif']:
             i = pyheif.read_heif(bytesIo)

             # Extract metadata etc
             for metadata in i.metadata or []:
                 if metadata['type']=='Exif':
                     # do whatever

             # Convert to other file format like jpeg
             s = io.BytesIO()
             pi = Image.frombytes(
                    mode=i.mode, size=i.size, data=i.data)

             pi.save(s, format="jpeg")
Found from here. git repo

Note, whatimage is a Python library to detect image type