-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHCEIC-to-PNG-CONVERTER.py
38 lines (32 loc) · 1.37 KB
/
HCEIC-to-PNG-CONVERTER.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import io
from PIL import Image
import pyheif
import whatimage
def decode_image(bytes_io):
"""Decodes the image from bytes and returns a PIL image."""
fmt = whatimage.identify_image(bytes_io)
if fmt in ['heic', 'avif']:
heif_file = pyheif.read_heif(bytes_io)
return Image.frombytes(mode=heif_file.mode, size=heif_file.size, data=heif_file.data)
else:
raise ValueError("Unsupported image format")
def convert_heic_to_png(folder_path):
"""Converts all HEIC images in the specified folder to PNG format."""
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.lower().endswith('.heic'):
file_path = os.path.join(root, file)
# Open the HEIC file in binary mode
with open(file_path, 'rb') as f:
bytes_io = f.read()
try:
img = decode_image(bytes_io)
# Define the output path with the PNG extension
output_path = os.path.splitext(file_path)[0] + '.png'
img.save(output_path, format="PNG")
print(f"Converted {file} to PNG.")
except ValueError as e:
print(f"Skipping {file}: {e}")
# Example usage:
# convert_heic_to_png("/path/to/your/folder")