Convert HEIC files to PNG files with Python

Photos taken with iPhone Live Photo are saved as HEIC files. Compared to JPEG and PNG, this file has less software and is difficult to handle. Therefore, make a note of how to convert the HEIC file to a PNG file.

You can run it from your browser using Google Colaboratory.

environment

Source code

Place conv.py in the same hierarchy as the file you want to convert and execute it.

conv.py


from PIL import Image
import pyheif

def conv(image_path):
    new_name = image_path.replace('heic', 'png')
    heif_file = pyheif.read(image_path)
    data = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
        )
    data.save(new_name, "PNG")

import glob
lst = glob.glob("*.heic")
for l in lst:
    conv(l)

Usage package

Install with the pip command.

pip install pyheif Pillow

Execution result

$ python conv.py
$ ls
IMG_3488.heic*  IMG_3494.heic*  IMG_3497.heic*  IMG_3499.heic*  IMG_3503.heic*  IMG_3510.heic*  IMG_3514.heic*  a.py*  
IMG_3488.mov*   IMG_3494.mov*   IMG_3497.mov*   IMG_3499.mov*   IMG_3503.mov*   IMG_3510.mov*   IMG_3514.mov*   env/

Reference URL

Convert HEIC file in specified directory to JPEG with Python --Qiita

Recommended Posts

Convert HEIC files to PNG files with Python
Convert PDF files to PNG files with GIMP
Convert svg file to png / ico with Python
Convert list to DataFrame with python
Convert memo at once with Python 2to3
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert FX 1-minute data to 5-minute data with Python
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
Made it possible to convert PNG to JPG with Pillow of Python
Convert files written in python etc. to pdf with syntax highlighting
Convert 202003 to 2020-03 with pandas
Convert FBX files to ASCII <-> BINARY in Python
Convert DICOM to PNG with Ascending and Descending
[Python] Convert from DICOM to PNG or CSV
Convert Windows epoch values to date with python
How to convert SVG to PDF and PNG [Python]
Upload files to Google Drive with Lambda (Python)
Convert multiple proto files at once with python
Convert strings to character-by-character list format with python
Convert jupyter notebook .ipynb files to python executable .py files
Connect to BigQuery with Python
How to convert / restore a string with [] in python
Procedure to load MNIST with python and output to png
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Connect to Wikipedia with Python
Post to slack with Python 3
Sorting image files with Python (2)
Sort huge files with python
How to convert JSON file to CSV file with Python Pandas
Sorting image files with Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Integrate PDF files with Python
PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
Reading .txt files with Python
Switch python to 2.7 with alternatives
Write to csv with Python
Convert python 3.x code to python 2.x
How to convert an array to a dictionary with Python [Application]
Convert images to sepia toning with PIL (Python Imaging Library)
How to upload files to Cloud Storage with Firebase's python SDK
Convert video to black and white with ffmpeg + python + opencv
Convert .ipynb to .html (with BatchFile)
Python: How to use async with
Recursively unzip zip files with python
Convert markdown to PDF in Python
[Python] Write to csv file with Python
Manipulating EAGLE .brd files with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Workflow to convert formula (image) to python
Decrypt files encrypted with OpenSSL with Python 3
MP3 to WAV conversion with Python
Convert sentences to vectors with gensim