Created
May 30, 2022 15:36
-
-
Save SWSRN/5f9b960df8294673bfa31138810756e2 to your computer and use it in GitHub Desktop.
Adds captions below pictures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################## | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment