Add Function To Mac Terminal Handle HEIC To JPEG In Bash

Last Edited: 2024-01-04 21:03:30

What is HEIC formatted pictures and why do I care?

Not so much with social media as HEIC format looks to be widely accepted among your facebooks, instagrams, snappoops, etc, however there are still many sites out there that do not allow this newish format.  I've personally had recent issues with craigslist, amazon reviews, poshmark, reverb, a few other sites I've tried selling things on.  I've more recently had this issue because my wife and I are making a smaller footprint, more on that later.

Obviously, it's very easy to take pics with my phone, airdrop™ them to my computer and post stuff for sale.  I don't know a lot of the particulars behind the newer HEIC format that my iPhone sometimes uses.  Sometimes the pictures wind up being in PNG format, but I haven't taken the time to see if there's a setting.  I guess I can do that right now.  Of course there is:

You can get here by going to General > Camera and choose Most Compatible.  Looks like they feel like HEIC will save some space, so that's the trade off.  Anyhow, I'd rather not change it and have to watch it as I've seen updated change settings at times.  

I wrote a quick function or alias to add to my bash login configuration file so that no matter where I am in the filesystem, I can run this command and it will find any HEIC (or heic) files and convert them to jpeg.  If you have a Mac, then there is already a built in image processor that you can use.  Even if you're not a super nerd like me, you can probably still get this to work in your system.

It uses Mac's built in program called "Sips" to do the convert.  To use this, you should have a file called .bashrc or .bash_profile in your home directory.  Open it up and put in this code by doing the following:

1. Open up your Terminal program.  It's in Applications > Utilities > Terminal - or click Command + Space Bar, and type Terminal and hit enter.

2. Unless you configured your bash shell to open in a different directory, you will be in your home directory.  If you had configured it, then you probably know how to go to your home directory.  Just in case, you can at this point type cd and hit enter.  Then you will be for sure.

3. Type ls -l .bash* to list out your bash configuration files, you should see either .bashrc or .bash_profile 

4. choose the file that's available; if both are there, I would use .bash_profile.  At this point you an open a text editor of your choice and pop the following code in the bottom of the file.  You can also use vim at the command line to edit it in terminal if you're comfortable.

# function call name

heic() {

 if [[ "$1" =~ "help" ]]; then

  echo "HEIC HELP"

  echo "Call $ heic [kill] [help] to run the program"

  echo "kill will remove the heic after convert"

  echo "WARNING: Removed file will not go to the recycle bin,"

  echo "it will go into the abyss and you will never see it again."

  echo "I love you."



 # search current dir for HEIC files

 for f in $(pwd)/*.{heic,HEIC}; do 

  # remove the extension

  BASENAME=$(echo "$f" | sed 's/\..*$//')

  # skip the wildcard if included in file listing

  if [[ "$BASENAME" != *"*"* ]]; then

   # get the new file name


   # make sure it doesn't exist

   if [ ! -f "$NEW" ]; then

    # use mac app sips to convert format to JPEG

    sips "$f" --setProperty format jpeg --out "$NEW"

    # if they asked for it, remove the HEIC file

    if [ "$1" == "kill" ]; then

     rm "$f";






5. Save your file and either type source .bash_profile, or close and reopen the terminal.

Simply put: Now you can navigate to any directory you want in terminal and type heic to automagically convert all of the heic files to jpeg.  You can also type heic kill to have it delete the old heic when it's done.

More completely put: What this does is create a function that you can call from the command line.  It looks in the present working directory that you're in for any files that have the .HEIC extension.  It looks to see if there's a JPEG version with the same name, and if not, runs the sips command and outputs the jpeg.  

If you add the word "kill" to the end of the command, it will also delete the HEIC file once the convert is complete.

// to covert all files in the current directory
$ heic
// to covert all files and remove the old ones
$ heic kill
// updated scripts also has help:
$ heic help
Call $ heic [kill] [help] to run the program
kill will remove the heic after convert
WARNING: Removed file will not go to the recycle bin,
it will go into the abyss and you will never see it again.
I love you.