from PIL import Image
import pillow_heif
import os

# Automatically register HEIF format with Pillow
pillow_heif.register_heif_opener()

def convert_heif_to_jpg(input_folder, output_folder):
    os.makedirs(output_folder, exist_ok=True)

    for filename in os.listdir(input_folder):
        if filename.lower().endswith(('.heif', '.heic')):
            input_path = os.path.join(input_folder, filename)
            output_path = os.path.join(output_folder, os.path.splitext(filename)[0] + '.jpg')

            try:
                with Image.open(input_path) as img:
                    rgb_img = img.convert("RGB")
                    rgb_img.save(output_path, "JPEG")
                print(f"Converted: {filename} -> {output_path}")
            except Exception as e:
                print(f"Failed to convert {filename}: {e}")

# Example usage:
convert_heif_to_jpg("D:/3D/Majka/heic", "D:/3D/Majka/jpg")

# convert_heif_to_jpg(r"D:\3D\Majka\heic", r"D:\3D\Majka\jpg") shpuld also work
# convert_heif_to_jpg("D:\\3D\\Majka\\heic", "D:\\3D\\Majka\\jpg") should also work
