Skip to content

Instantly share code, notes, and snippets.

@Chhunneng
Created October 23, 2023 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chhunneng/a05d499ccbcb2a9e7f1a34d8d7ba2cdf to your computer and use it in GitHub Desktop.
Save Chhunneng/a05d499ccbcb2a9e7f1a34d8d7ba2cdf to your computer and use it in GitHub Desktop.
This code is use to convert any image extension with ImageMagick
from PIL import Image
temp_original_picture_path = "./image.HEIC"
temp_converted_picture_path = "./image.png"
# make sure you install ImageMagick in your machine
# https://imagemagick.org/script/download.php
try:
import pillow_avif
from pillow_heif import register_heif_opener
register_heif_opener()
image = Image.open(temp_original_picture_path)
converted_image = image.convert("RGB")
converted_image.save(temp_converted_picture_path, format="PNG")
except Exception:
try:
from wand.image import Image as WandImage
with WandImage(filename=temp_original_picture_path) as image:
image.format = "png"
image.save(filename=temp_converted_picture_path)
except Exception:
print("error")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment