How to work with HEIC image file types in Python

后端 未结 6 996
醉梦人生
醉梦人生 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:38

    Adding to the answer by danial, i just had to modify the byte array slighly to get a valid datastream for further work. The first 6 bytes are 'Exif\x00\x00' .. dropping these will give you a raw format that you can pipe into any image processing tool.

    import pyheif
    import PIL
    import exifread
    
    def read_heic(path: str):
        with open(path, 'rb') as file:
            image = pyheif.read_heif(file)
            for metadata in image.metadata or []:
                if metadata['type'] == 'Exif':
                    fstream = io.BytesIO(metadata['data'][6:])
    
        # now just convert to jpeg
        pi = PIL.Image.open(fstream)
        pi.save("file.jpg", "JPEG")
    
        # or do EXIF processing with exifread
        tags = exifread.process_file(fstream)
    

    At least this worked for me.

提交回复
热议问题