PEIF image file and python analysis

1 Introduction

High efficiency image file format (English: High Efficiency Image File Format, HEIF; also known as high efficiency image file format [1]) is a file format used for single images or image sequences. It was developed by the Motion Picture Experts Group (MPEG) and defined in MPEG-H Part 12 (ISO/IEC 23008-12).

The HEIF specification also defines a storage method for high-efficiency video coding (HEVC)-encoded embedded images and HEVC-encoded image sequences, in which inter-frame prediction is applied in a constrained manner.

HEIF files are compatible with ISO basic media file formats (ISOBMFF, ISO/IEC 14496-12), and can also include other media streams, such as timed text and audio.

.heic is just an extension of the HEIF file format. The implication is: HEIF not only has the extension of .heic, but also others, such as .heif and .avci, which are all HEIF file formats. Of course, the only common ones are .heif and .heic, while .avci is rare.

In Apple's implementation, a single picture uses the .heic file extension, and it uses the HEVC encoding format by default. Of course, Apple will officially support H.264/MPEG-4 AVC encoded .avci files and .heif files in the future.

2 Install heif package

# centos
yum install libffi libheif-devel libde265-devel

# mac
brew install libheif

Install python package

pip install pyheif

use




import whatimage
import pyheif
from io import BytesIO
from PIL import Image

def read_image_heif(img_file, to_fmt='JPEG'):
    heif_file = pyheif.read(img_file)
    image = Image.frombytes(heif_file.mode,
                            heif_file.size,
                            heif_file.data,
                            "raw",
                            heif_file.mode,
                            heif_file.stride,
                            )
    # image.save('5.jpg', 'JPEG')
    with BytesIO() as out:
        image.save(out, format=to_fmt)
        img_file = out.getvalue()
    return img_file


def main():
    img_file = open('/home/zhangxin/gitlab_md/urs-client/mit/5.png', 'rb').read()
    fmt = whatimage.identify_image(img_file)
    if fmt in ['heic']:
        img_file = read_image_heif(img_file)
        with open('5.jpg', 'wb') as fo:
            fo.write(img_file)

if __name__ == '__main__':
    main()

3 Reference documents

Guess you like

Origin blog.csdn.net/sdlypyzq/article/details/108398887