Created
October 23, 2023 02:22
-
-
Save Chhunneng/a05d499ccbcb2a9e7f1a34d8d7ba2cdf to your computer and use it in GitHub Desktop.
This code is use to convert any image extension with ImageMagick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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