How to work with HEIC image file types in Python

后端 未结 6 1004
醉梦人生
醉梦人生 2021-01-31 18:08

The High Efficiency Image File (HEIF) format is the default when airdropping an image from an iPhone to a OSX device. I want to edit and modify these .HEIC files with Python.

6条回答
  •  失恋的感觉
    2021-01-31 18:22

    You guys should check out this library, it's a Python 3 wrapper to the libheif library, it should serve your purpose of file conversion, extracting metadata:

    https://github.com/david-poirier-csn/pyheif

    https://pypi.org/project/pyheif/

    Example usage:

     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")
    
      ...
    

提交回复
热议问题