Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add captions to images
#!/bin/bash
# Give a tab-separated file of filenames for images and captions (captions.tsv), add the captions
# at the bottom of the corresponding images. Requires that identity and imagemagick be installed.
set -e
#set -x
DIR=captioned
mkdir -p $DIR
while read line
do
FILE=$(cut -f1 <<< $line)
CAPTION=$(cut -f2 <<< $line | tr -d '\r')
echo "$FILE"
NEW_FILE=$DIR/$FILE
rm -f "$NEW_FILE"
WIDTH=$(identify -format %w "$FILE")
POINT_SIZE=$(expr $WIDTH / 25)
#POINT_SIZE=20
echo " Width $WIDTH, point size $POINT_SIZE"
BORDER=$(expr $POINT_SIZE / 2);
WIDTH_WITHOUT_BORDER=$(expr $WIDTH - 2 \* $BORDER)
convert \( -background white -fill black -pointsize $POINT_SIZE -size ${WIDTH_WITHOUT_BORDER}x \
caption:"$CAPTION" -bordercolor white -border $BORDER \) "$FILE" +swap -append "$NEW_FILE"
#xdg-open $NEW_FILE
done < captions.tsv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.