#!/bin/bash # Build instructions: # https://medium.com/@eplt/5-minutes-to-install-imagemagick-with-heic-support-on-ubuntu-18-04-digitalocean-fe2d09dcef1 # Command: # https://zwbetz.com/convert-heic-images-to-jpg/ # trap ctrl-c and call ctrl_c() # https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash trap ctrl_c INT function ctrl_c() { echo "${rst}${red}CTRL+C Detected, exiting.${rst}" exit } padding=" " if [[ "${@}" == *"-h"* ]]; then printf "%s\n" "Command usage:" printf "$padding%-20s%s\n" "--dir [directory]" "Specify a directory other than current" printf "$padding%-20s%s\n" "-d --delete" "Delete original files (default keeps originals)" printf "$padding%-20s%s\n" "-q --quiet" "Suppresses progress output (summaries are still displayed)" printf "$padding%-20s%s\n" "[--png|--jpg]" "Save as jpg/png (default is jpg)" printf "$padding%-20s%s\n" "-h" "Print this help message and exit" exit 0 fi # DEFAULTS # The folder with the files you want to convert directory="`pwd`" # Set to 0 to delete the original .heic files after they are converted keeporiginals=1 # Set to 0 for PNG (much larger file size) saveasjpg=1 # Suppress progress output keepquiet=0 customdir=0 for _arg in ${@}; do if [ $customdir -eq 1 ]; then directory="$_arg"; customdir=2; continue; fi if [ "$_arg" == "--dir" ]; then customdir=1; continue; fi if [ "$_arg" == "-d" ]; then keeporiginals=0; continue; fi if [ "$_arg" == "--delete" ]; then keeporiginals=0; continue; fi if [ "$_arg" == "-q" ]; then keepquiet=1; continue; fi if [ "$_arg" == "--quiet" ]; then keepquiet=1; continue; fi if [ "$_arg" == "--png" ]; then saveasjpg=0; continue; fi if [ "$_arg" == "--jpg" ]; then saveasjpg=1; continue; fi done # Make sure the mogrify command is available. If this is being # run as a cron job the which command will fail so we check the # default location just in case. which mogrify > /dev/null if [[ $? -eq 1 ]]; then if [[ ! -e "/usr/local/bin/mogrify" ]]; then echo "Unable to run the mogrify command. Please follow the build and installation instructions at the top of the script to install the program on your system." exit fi fi # summary: printf "\n%s\n" "Summary:" printf "$padding%s\n" "Directory: $directory" [ $keeporiginals -eq 0 ] && printf "$padding%s\n" "Originals: Delete" || printf "$padding%s\n" "Originals: Keep" [ $keepquiet -eq 0 ] && printf "$padding%s\n" "Output: Visible" || printf "$padding%s\n" "Output: Hidden" [ $saveasjpg -eq 0 ] && printf "$padding%s\n" "Save as: PNG" || printf "$padding%s\n" "Save as: JPG" printf "\n%s" "Press enter to begin or ^c to exit" read filecount=0 [ $saveasjpg -eq 0 ] && destformat="png" || destformat="jpg" # Loop over all the .heic files in the folder for heic in *.heic; do # Drop the .heic extension and append .jpg instead img=`echo "$heic" | rev | cut -d. -f2- | rev`.$destformat if [ -f "$img" ]; then [ $keepquiet -eq 0 ] && printf "%s\n" "Skipping $heic ($img exists)" else [ $keepquiet -eq 0 ] && printf "%s\n" "Converting $heic to $img"; fi [ -f "$img" ] || let filecount=filecount+1 # If the file we are going to make does not exist, run the command to convert it [ -f "$img" ] || /usr/local/bin/mogrify -format $destformat "$heic" # If we don't want the originals, give them the axe [ $keeporiginals -eq 0 ] && rm "$heic" done if [ $filecount -gt 0 ]; then printf "\n%s\n\n" "Script complete. Converted $filecount heic files to $destformat" else if [ $customdir -eq 2 ]; then printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you specified the right directory?" else printf "\n%s\n\n" "Found 0 heic files to convert. Are you sure you're in the right directory?" fi