Bulk Converting HEIC to JPG in Linux

With each revision of hardware, Apple continually makes this more difficult for those that have to interact with Apple fans. One of the most annoying is the HEIC format for photos. Even Apple’s own Photos app in iCloud won’t allow HEIC photo uploads, so to make any of these photo useful the HEIC files need to be converted to JPEG form (.JPG).

Normally people take a lot of photos, so the conversion of the files is best done in bulk. Here are some tips for dealing with these bulk files.

The first step is to install a tool that that can handle the conversion. In a Debian based distro, run

sudo apt install libheif-examples

For other distro’s, take a look at this linuxnightly.com article.

Download all the images you want to convert from the device, or a service like Google photos and save them all in the one folder. If you’ve had to download them in multiple ZIP files (due to limits like Google Photos only allowing a maximum of 500 images at a time) you can extract the ZIP files with a command like

for f in *.zip; do unzip $f -d pending; done

which will loop through all the ZIP files in the current directory and extract the files from each. (Run sudo apt-get install unzip if you don’t have the unzip command already.)

Once you have all the HEIC files in the same directory, run the conversion command using the same for loop.

for f in *.HEIC; do heif-convert -q 95 $f $f.jpg; done

The -q 95 sets the image quality of the JPEG output, with 95 being a good balance between file size and quality. You could use 100 for maximum quality, or drop it to 90 or 80 if you don’t mind a quality drop for significant disk space savings.

This will give you a bunch of .JPG files in the same folder, some of which may have HDR (High Dynamic Range) mapping files with them. If you don’t care about the HDR mapping, these can be removed with

rm *aux:hdrgainmap.HEIC.jpg

To make further manipulation easier, you may want to create directory for the resultant JPEG files and move these into the new directory.

mkdir jpeg
mv *.jpg jpeg/
cd jpeg

If there are thousands of files to deal with, uploading them may be best done in lots of 500. To make this easier to handle, create some subdirectories to store the batches, then move 500 files at a time into the new subdirectory, repeating until all files have been moved.

mkdir batch1
i=1; for f in *.JPG; do mv $f ./batch1; i=$((i+1)); if [[ "$i" -gt 500 ]]; then break; fi; done
mkdir batch2
i=1; for f in *.JPG; do mv $f ./batch2; i=$((i+1)); if [[ "$i" -gt 500 ]]; then break; fi; done
mkdir batch3
.
.
.

The above commands will use the counter variable i, incrementing it until 500 is reached. If you want smaller batches, reduce the 500 to give you batches that you can easily upload to the intended platform, such as Apple’s iCloud Photos page.

Advanced tip

If you have HEIF files scattered throughout some directories, use the find command to convert all the files.

find . -iname "*.heic" -exec heif-convert -q 95 {} {}.jpg \;