Package: pyheif
pyheifPython 3.6+ interface tolibheiflibrary using CFFINote: currently only reading is supported.InstallationSimple installation - Linux (installs manylinux2014 wheel, doesn't work with Alpine)pip install --upgrade pip
pip install pyheifInstalling from source - MacOSbrew install libffi libheif
pip install git+https://github.com/carsales/pyheif.gitInstalling from source - Linuxapt install libffi libheif-dev libde265-devoryum install libffi libheif-devel libde265-develthenpip install git+https://github.com/carsales/pyheif.gitInstalling from source - WindowsSorry, not going to happen!UsageRead the primary image of a HEIF encoded fileThepyheif.read(path_or_bytes)function can be used to read the primary image of a HEIF encoded file. It can be passed any of the following:A string path to a file on diskApathlib.Pathpath objectA Pythonbytesorbytearrayobject containing HEIF contentA file-like object with a.read()method that returns bytesIt returns aHeifFileobject.importpyheif# Using a file path:heif_file=pyheif.read("IMG_7424.HEIC")# Or using bytes directly:heif_file=pyheif.read(open("IMG_7424.HEIC","rb").read())Converting to a Pillow Image objectIf your HEIF file contains an image that you would like to manipulate, you can do so using thePillowPython library. You can convert aHeifFileto a Pillow image like so:fromPILimportImageimportpyheifheif_file=pyheif.read("IMG_7424.HEIC")image=Image.frombytes(heif_file.mode,heif_file.size,heif_file.data,"raw",heif_file.mode,heif_file.stride,)Note: themodeproperty is passed twice - once to themodeargument of thefrombytesmethod, and again to themodeargument of therawdecoder.You can now use any Pillow method to manipulate the file. Here's how to convert it to JPEG:image.save("IMG_7424.jpg","JPEG")Read the entire container within the HEIF fileThepyheif.open_container(path_or_bytes)function can be used to read the HEIF container from a HEIF encoded file. It takes the same parameter aspyheif.read()It returns aHeifContainerobject.ObjectsThe HeifFile objectTheHeifFilehas the following properties:mode- the image mode, e.g. "RGB" or "RGBA"size- the size of the image as a(width, height)tuple of integersdata- the raw decoded file data, as bytesmetadata- a list of metadata dictionariescolor_profile- a color profile dictionarystride- the number of bytes in a row of decoded file databit_depth- the number of bits in each component of a pixelThe UndecodedHeifFile objectThis is a HEIF image that has not been decoded. Calling theUndecodedHeifFile.load()method will load the data and the object will become aHeifFileThe HeifContainer objectTheHeifContainerhas the following properties:primary_image- theHeifTopLevelImageobject of the primary image in the file.top_level_images- a list of allHeifTopLevelImageobjects in the file.The HeifTopLevelImage objectTheHeifTopLevelImagehas the following properties:id- the id of the imageimage- theUndecodedHeifFileorHeifFileobject of the imageis_primary- is this the primary image in the containerdepth_image- theHeifDepthImageif there is oneauxiliary_images- a list ofHeifAuxiliaryImageobjectsThe HeifDepthImage objectTheHeifDepthImagehas the following properties:id- the id of the imageimage- theUndecodedHeifFileorHeifFileobject of the imageThe HeifAuxiliaryImage objectTheHeifAuxiliaryImagehas the following properties:id- the id of the imageimage- theUndecodedHeifFileorHeifFileobject of the imagetype- a string indicating the type of auxiliary image