Categories
Productivity Tech

Linux: How To Convert HEIC Files to JPG or PNG

As some of you might know, iPhone has its own picture file format. The HEIC(High-Efficiency Image Container) format. it doesn’t always work when you want to upload it to many websites. There are some websites out there that can convert it for you. It might be okay for some photos that you don’t care about. But for some family photos, you might not want to share them on some random websites.

I found that on Linux, you can simply install a command-line tool called heif-convert. To use it, simply install the libheif-examples command-line. heif-convert just comes with it for free. To install, run the following command:

sudo apt-get install libheif-examples

You can easily convert your HEIC file with the following command:

heif-convert [original-file-name] [file-name-with-jpg-or-png-extension]

For example, if you have a file called IMG_1234.HEIC and want to convert it to IMG_1234.jpg, you can simply run:

heif-convert IMG_1234.HEIC IMG_1234.jpg

Easy, isn’t it? This command can only convert 1 file at a time, which can be annoying when you need to convert multiple files. If you’re lucky, all of the files you drag from the iPhone will have lowercase extension “heic”. You can run this script to convert them:

for file in *.heic; do heif-convert $file ${file/%.heic/.jpg}; done

Sometimes, some files are in lowercase and some come with uppercase. There are many ways to deal with this issue. You can rename all the files to lowercase and then run the above command, you can write a program that reads all the files and then run the heif-convert command.

I took the route to learn about how to do that with a bash script. My script will read all the files that contain “.heic” or “.HEIC” in the filename. Then convert them into jpg format with the heif-convert command.

I created a GitHub repository for any handy command in the future. You can download the heic-converter.sh executable from here. To run it:

./heic-converter.sh

If my note helped, please consider buying me a coffee😁.

Cheers,
Lok

Reference:

https://askubuntu.com/questions/958355/any-app-on-ubuntu-to-open-and-or-convert-heif-pictures-heic-high-efficiency-i

5 replies on “Linux: How To Convert HEIC Files to JPG or PNG”

Hi Lok,

Thanks for the post. Has helped me out with the wife’s photos.
As you took the time to learn to write a bash I thought you might be interested in learning about xargs. You can do what your bash script does with something like:
ls *.HEIC | xargs -P4 -I% bash -c ‘heif-convert $1 ${1/.HEIC/.jpg}’ — %

The main advantage of this is the -P4 option tells xargs to run 4 of these in parallel. I have an 8 thread laptop so running 4 in parallel leaves 4 for doing other work.

The ${file/.heic/.jpg} syntax is not something I have seen before. Very neat.

Thanks again for your work.
Cheers.
–JohnM

I have this set up with a inotifywait script to watch a folder for when I move a .HEIC file in.
With the test images I used, I was able to convert them immedaitely.
But when I used an image from an Iphone I started to get a weird bug.
An auxiliary image is being created, and is in black and white for some reason.

Also, both the new jpg and the aux image are being rotated 90 degrees.

Neither is a dealbreaker, just something strange that I’d like to prevent if possible.

THAT SAID. This tool is great, and very useful, thank you for the work you’e put in.

I decided to use imagemagic for converting. This soft don’t rotate image, and seems works faster.
There is my script

FOLDER=@

find “$FOLDER” -maxdepth 1 -regex ‘.*\.HEIC\|.*\.heic’ -print0 | xargs -0 -P6 -I % bash -c ”’
convert “$1” -quality 100 -verbose “${1/.*/.jpg}” && \
mv -v “$1″ path/to/heic_trash
”’ — %

This output:
/folder/20230221_174140.HEIC=>/folder/20230221_174140.jpg HEIC 3024×4032 3024×4032+0+0 8-bit sRGB 6.31703MiB 1.680u 3:33.218
renamed ‘/folder/20230221_174140.HEIC’ -> ‘/heic_trash/20230221_174140.HEIC’

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.