Convert HEIC to JPG on Windows Using Python

Introduction

HEIC (High Efficiency Image Coding) is a format used for images, especially by Apple devices. Converting HEIC to JPG can be challenging on Windows, but with the help of Python and the pillow-heif library, this task becomes manageable. This guide will walk you through the process of setting up the environment and converting all HEIC files in a folder to JPG.

Using a Python script to convert HEIC files to JPG has several advantages compared to using an online HEIC converter website:

  1. Batch Processing: The script allows you to convert multiple HEIC files in a folder at once, saving you time and effort compared to uploading and converting files one by one on a website.
  2. Privacy: Your images remain on your local machine, reducing the risk of privacy breaches or data leaks associated with uploading files to an online service.
  3. Automation: The script can be easily integrated into larger workflows or automated tasks, making it ideal for repetitive conversions without manual intervention.
  4. Customization: You can modify the script to fit your specific needs, such as changing output formats, adjusting image quality, or adding other processing steps.
  5. No Internet Required: Once the script and necessary libraries are set up, you can convert files without needing an internet connection, which is useful in environments with limited or no internet access.
  6. Cost-Effective: Using a local script avoids potential costs associated with premium online conversion services, especially if you need to convert a large number of files frequently.
  7. Speed: Depending on your internet connection, converting files locally might be faster since you avoid the time needed to upload and download files from a web service.

Prerequisites

  1. Python: Ensure Python is installed on your system. You can download it from python.org.
  2. pip: Python’s package installer should be installed. It comes with Python, but you can check by running pip --version in your command prompt.

Lets get started create a new project and within that project is where the heic files will need to be stored!

How to convert heic to jpg step by step Guide

  1. Prepare Your Folders Create two folders in your working directory:
  • heic_files: This folder will contain the HEIC files you want to convert.
  • png_files: This folder will store the converted PNG files.
  • I have added the heic files to the heic_files folder ready to be converted into jpg or png!
  1. Install Required Libraries Open your command prompt and run the following command to install the pillow-heif library:
   pip install pillow-heif
  1. Python Script to Convert HEIC to PNG Create a new Python script file (e.g., convert_heic_to_png.py) and paste the following code into it:
   import os
   from PIL import Image
   import pillow_heif

   def heic_to_png(input_path, output_path):
       # Register HEIF plugin
       pillow_heif.register_heif_opener()

       # Open HEIC file
       image = Image.open(input_path)

       # Save as PNG
       image.save(output_path, "JPEG")
       print(f"Converted {input_path} to {output_path}")

   def convert_all_heic_to_png(input_folder, output_folder):
       # Create output folder if it doesn't exist
       if not os.path.exists(output_folder):
           os.makedirs(output_folder)

       # Iterate over all files in the input folder
       for filename in os.listdir(input_folder):
           if filename.lower().endswith('.heic'):
               input_path = os.path.join(input_folder, filename)
               output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
               heic_to_png(input_path, output_path)

   # Example usage
   input_folder = 'heic_files'
   output_folder = 'jpg_files'
   convert_all_heic_to_png(input_folder, output_folder)
  1. Run the Script Ensure you have your HEIC files in the heic_files folder. Then, run the script by navigating to the directory containing your script and running the following command in the command prompt or press the play button in pycharm/ide:
   python convert_heic_to_jpg.py

The script will convert all HEIC files in the heic_files folder to PNG format and save them in the jpg_files folder.

Conclusion

You now have a working setup to convert HEIC files to JPG using Python on Windows.

Converting HEIC to PNG on Windows Using Python

Introduction

Now lets do the same with converting HEIC to PNG which follows the same prerequisites and steps but the only difference is the code!

  • Create a new directory folder called png_files to store the converted png files!
  • Python Script to Convert HEIC to PNG Create a new Python script file (e.g., convert_heic_to_png.py) and paste the following code into it:
   import os
   from PIL import Image
   import pillow_heif

   def heic_to_png(input_path, output_path):
       # Register HEIF plugin
       pillow_heif.register_heif_opener()

       # Open HEIC file
       image = Image.open(input_path)

       # Save as PNG
       image.save(output_path, "PNG")
       print(f"Converted {input_path} to {output_path}")

   def convert_all_heic_to_png(input_folder, output_folder):
       # Create output folder if it doesn't exist
       if not os.path.exists(output_folder):
           os.makedirs(output_folder)

       # Iterate over all files in the input folder
       for filename in os.listdir(input_folder):
           if filename.lower().endswith('.heic'):
               input_path = os.path.join(input_folder, filename)
               output_path = os.path.join(output_folder, f"{os.path.splitext(filename)[0]}.png")
               heic_to_png(input_path, output_path)

   # Example usage
   input_folder = 'heic_files'
   output_folder = 'png_files'
   convert_all_heic_to_png(input_folder, output_folder)
  1. Run the Script Ensure you have your HEIC files in the heic_files folder. Then, run the script by navigating to the directory containing your script and running the following command in the command prompt or clicking the Run 'convert_heic_to_png' option in your ide/pycharm:
   python convert_heic_to_png.py

The script will convert all HEIC files in the heic_files folder to PNG format and save them in the png_files folder.

Conclusion

You now have a functional setup for converting HEIC files to JPG and HEIC files to PNG using Python on Windows. This guide aims to help you efficiently streamline the conversion process. If you encounter any issues or need further assistance, feel free to seek help from the Python community or refer to the documentation of the libraries used.

5 thoughts on “Convert HEIC to JPG on Windows Using Python

  1. Hello there! I know this is somewhat off topic but I was wondering which blog
    platform are you using for this site? I’m getting tired of WordPress because I’ve had
    problems with hackers and I’m looking at options for another
    platform. I would be fantastic if you could point me in the direction of a good platform.

  2. I’ve mistakenly changed my admin priveleges on one of my blogs to “guest”. I need the blogger people to restore it. I cannot find an actual email or contact form anywhere, and I’ve googled everything, including the Blogger Group on Google Groups, where they try to push everyone who needs support. I would gladly pay them for better service..

Leave a Reply

Your email address will not be published. Required fields are marked *