How to Add a Label – Use Shell Script

How to install script tag_cap_images.sh which makes “captioned” images with metadata below the picture. Calls exiftool and ImageMagick.

Oh, the horror of it!  You might need a computer scientist to lead you through this.

Download it from here:

https://gist.github.com/SWSRN/5f9b960df8294673bfa31138810756e2

Or cut and paste the code below (fingers crossed about the formatting.)

“#” is the comment character except for the first line “#!/bin/bash”.

Copy the following code into a new document in TextEdit. Click Format>Make plain text from the menu bar. Save it to the bin sub directory of your home directory that you created above. I called it  ~/bin/tag_cap_images.sh

##############################################
# tag_cap_images.sh
#
# Create a new JPEG image from an old one with 
# 1.	A white border with extra space at the bottom
# 2.	A generalized caption made up of the following metadata from the original photo,
#	 perhaps added or edited in Lightroom or other software:
# 	a.	title
# 	b.	caption (may be identical to “description” metadata)
# 	c.	the keywords/tags (a.k.a. “subject” metadata)
# 	d.	The originating filename (from within Lightroom) and the final filename
# 3.	The final image with caption is larger than the original photo. 
#	But with the bonus that if you crop down to the photo, 
#	the dimensions are the same as the original photo. 
# 4.	The final image’s metadata has been automatically copied over from the original photo,
#	i.e., the title, caption, keywords, filename, etc. .
# 
# Does not work with input *.HEIC and raw files *.dng, *.ARW and *.CR2.
#  Seems to only work with JPEG.
#
# Usage
# % bash tag_cap_images.sh image1, image2, ...
#
# For example. Input could be most any image file format. 
#  % cd yourname/whereYourPicturesAre
#  % bash ~/bin/tag_cap_images.sh *.jpg
#
# output is always JPEG and is in 
# yourname/whereYourPicturesAre-captioned/capsh_*.jpg.  
#
# If you don't like the output folder location or the output file name, change the script.
##############################################

#!/bin/bash

FONT="/System/Library/Fonts/Supplemental/Times New Roman.ttf"
#FONT="/System/Library/Fonts/Supplemental/Arial Unicode.ttf"

# Exit on error
set -e

# output to subdirectory
#OUTPUTPATH=captioned

# output dir from the command line (first argument is directory)
#OUTPUTPATH=$1
#shift 1 

# output to sister directory ...-captioned
OUTPUTPATH=$PWD-captioned

echo OUTPUTPATH $OUTPUTPATH
mkdir -p "$OUTPUTPATH"
#echo Files: "$@"
for var in "$@"; do
    echo Processing $var
    OUTVAR="capsh_"$var
    WIDTH=$(exiftool -s -s -s -ExifImageWidth "$var")
    HEIGHT=$(exiftool -s -s -s -ExifImageHeight "$var")
    if test z"$WIDTH" = z; then
        WIDTH=$(exiftool -s -s -s -ImageWidth "$var")
        HEIGHT=$(exiftool -s -s -s -ImageHeight "$var")
    fi
    echo WIDTH: $WIDTH HEIGHT: $HEIGHT
    # assume larger dimension is mapped to 10" for approximate 8X10 image.
    DPI=$(python3 -c "print(max($WIDTH,$HEIGHT)//10.0)") 
    #echo DPI $DPI
    BORDER=$(python3 -c "print(int(round(0.025*max($WIDTH,$HEIGHT))))")
    FONTSIZE=$(python3 -c "print(int(round(0.015*($DPI*10.0))))") # in pixels
    FONTSIZE=$(python3 -c "print(int(min(99,$FONTSIZE)))")        # max is 99 pixels
    TITLE=$(exiftool -s -s -s -Title "$var")
    echo short TITLE: $TITLE
    if test z"$TITLE" = z; then
        LABEL=$(exiftool -s -s -s -Description -Subject -FileName "$var")
        LABEL="$LABEL -> $OUTVAR   tag_cap_images.sh"
#        LABEL=$(exiftool -s -s -s -Description -Subject -FileName "$var")
    else
        LABEL="\"$TITLE\"\n$(exiftool -s -s -s -Description -Subject -FileName "$var")"
        LABEL="$LABEL -> $OUTVAR    tag_cap_images.sh"
#        LABEL="\"$TITLE\"\n$(exiftool -s -s -s -Description -Subject -FileName "$var")"
    fi
    echo long LABEL: $LABEL
    echo Font: $FONTSIZE pixels  Border: $BORDER pixels
    magick convert "$var" \
    -pointsize $FONTSIZE \
    -font "$FONT" \
    -background white \
    \( -size $WIDTH "caption:$LABEL" \) \
    -bordercolor white \
    -border ${BORDER}x$BORDER \
    -smush -$BORDER \
    "$OUTPUTPATH/$OUTVAR"
    echo " "
done
  1. Call your friendly computer scientist
  2. Install exiftool (see where we installed LRmogrify2)
  3. Install imageMagick (see where we installed LRmogrify2)
  4. Open the terminal app from launch pad on a Mac.
  5. Note: the command in the next item might fail if there is any white space in your file names.
  6. To create captioned images from ALL the JPEG files in your directory at once, do the following. (Other image types will work except Apple’s *.HEIC file format is too recent.)  Type in the terminal (note: this example is for JPEG input files.)
  7. cd yourname/whereYourPicturesAre
  8. bash ~/bin/tag_cap_images.sh *.jpg
  9. All your new captioned images will be JPEGs in the new directory yourname/whereYourPicturesAre-captioned