How to Convert From HEIC to JPG in Python on WIndows

Question:

Im trying to convert HEIC to JPG using python. The only other answers about this topic used pyheif. I am on windows and pyheif doesn’t support windows. Any suggestions? I am currently trying to use pillow.

Asked By: Stephan Yazvinski

||

Answers:

As of today, I haven’t found a way to do this with a Python-only solution. If you need a workaround, you can find any Windows command line utility that will do the conversion for you, and call that as a subprocess from Python.

Here is an example option using PowerShell: https://github.com/DavidAnson/ConvertTo-Jpeg

It’s also pretty easy these days to write a .NET-based console app that uses Magick.NET. That’s what I ended up doing.

Answered By: Jeff

Just was looking at the same topic. I came across this:

https://pypi.org/project/heic-to-jpg/

I haven’t had time to look more into this, but thought I’d share this.

Answered By: Volsie711

code below convert and save the picture as png format

from PIL import Image
import pillow_heif

    heif_file = pillow_heif.read_heif("HEIC_file.HEIC")
    image = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
    
    )

    image.save("./picture_name.png", format("png"))
Answered By: adel

In the latest version of pillow_heic module, below code will work fine. only read_heif is replaced with read.

from PIL import Image

import pillow_heif

heif_file = pillow_heif.read(r"E:image20210914_150826.heic")

image = Image.frombytes(
    heif_file.mode,
    heif_file.size,
    heif_file.data,
    "raw",

)

image.save(r"E:imagetest.png", format("png"))
Answered By: deepesh

Please, use open_heif or PIL.Image.open() if you need some Pillow things to do with image.

pillow-heif supports lazy loading of data.

read_heif and read are slow for files containing multiply images, it will decode all of them during call.

P.S.: I am the author of pillow-heif.

Answered By: Alexander Piskun

I use this code to convert an image from a form from heic to jpeg a before I save it to a local file system.

This code does some renaming, so it can be saved as FileStorage object in the db with access to filename and mime type.

As function of the Class Converter.

import io
from PIL import Image
import pillow_heif
from werkzeug.datastructures import FileStorage

class Converter:

    def convert_heic_to_jpeg(self, file):
        # Check if file is a .heic or .heif file
        if file.filename.endswith(('.heic', '.heif', '.HEIC', '.HEIF')):
            # Open image using PIL
            # image = Image.open(file)

            heif_file = pillow_heif.read_heif(file)
            image = Image.frombytes(
                heif_file.mode,
                heif_file.size,
                heif_file.data,
                "raw",
            )

            # Convert to JPEG
            jpeg_image = image.convert('RGB')

            # Save JPEG image to memory temp_img
            temp_img = io.BytesIO()
            jpeg_image.save(temp_img, format("jpeg"))

            # Reset file pointer to beginning of temp_img
            temp_img.seek(0)

            # Create a FileStorage object
            file_storage = FileStorage(temp_img, filename=f"{file.filename.split('.')[0]}.jpg")

            # Set the mimetype to "image/jpeg"
            file_storage.headers['Content-Type'] = 'image/jpeg'

            return file_storage
        else:
            raise ValueError("File must be of type .heic or .heif")
Answered By: Tonkyboy

Work for me.

from PIL import Image
import pillow_heif


pillow_heif.register_heif_opener()

img = Image.open('c:image.HEIC')
img.save('c:image_name.png', format('png'))
Answered By: muxa