Skip to content

Instantly share code, notes, and snippets.

@robes7
Last active July 15, 2025 16:24
Show Gist options
  • Select an option

  • Save robes7/28b47ea11353c504ecaa745a010ccfe5 to your computer and use it in GitHub Desktop.

Select an option

Save robes7/28b47ea11353c504ecaa745a010ccfe5 to your computer and use it in GitHub Desktop.
HEIC to PNG Converter Script in Python
# HEIC to PNG Converter Script
#
# This script will:
# 1. Check if required packages (Pillow, pillow-heif) are installed, and install them if they aren't.
# 2. Prompt the user for a folder path containing HEIC files.
# 3. Create a "Converted" subfolder for output.
# 4. Convert all HEIC images from the specified folder to PNG format in the "Converted" subfolder.
# 5. Print progress messages for successful conversions.
import importlib
import subprocess
import sys
# List of required packages
required = ['Pillow', 'pillow-heif']
for package in required:
try:
importlib.import_module(package if package != 'Pillow' else 'PIL')
except ImportError:
print(f"Package '{package}' not found. Installing...")
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
from PIL import Image
import pillow_heif
import os
def convert_heic_to_png(folder_path):
output_folder = os.path.join(folder_path, 'Converted')
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(folder_path):
if filename.lower().endswith('.heic'):
heic_path = os.path.join(folder_path, filename)
heif_file = pillow_heif.read_heif(heic_path)
image = Image.frombytes(
heif_file.mode,
heif_file.size,
heif_file.data,
"raw",
heif_file.mode,
heif_file.stride,
)
png_filename = os.path.splitext(filename)[0] + '.png'
png_path = os.path.join(output_folder, png_filename)
image.save(png_path, 'PNG')
print(f"Converted {filename} to {png_filename}")
folder_path = input('Enter the folder path where HEIC files are located: ')
convert_heic_to_png(folder_path)
print('Conversion completed.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment