Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active November 1, 2025 01:42
Show Gist options
  • Select an option

  • Save ckhung/ed1c32ac0bf644651f667f009a6b0e35 to your computer and use it in GitHub Desktop.

Select an option

Save ckhung/ed1c32ac0bf644651f667f009a6b0e35 to your computer and use it in GitHub Desktop.
screenshot caption concatenation
#!/bin/bash
# screenshot caption concatenation
# assumption:
# 1. all screenshot files are of the same extension, of the same size, with ordered file names.
# 2. there are no other files with names sscapcat[059]-* in /tmp.
read -r x_pad y_header y_content y_caption out_file <<<$(echo 0 0 0 0 result.jpg)
while getopts 'x:y:o:h' opt; do
case "$opt" in
x) x_pad="$OPTARG" ;;
y) IFS=, read -r y_content y_caption y_tail <<< $OPTARG ;;
o) out_file="$OPTARG" ;;
?|h)
echo "Usage: $(basename $0) [-x x_pad] [-y y_content,y_caption,y_tail]"
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
f_head="$1"
shift
f_tail="${@: -1}"
set -- "${@:1:$(($#-1))}"
echo "$x_pad $y_content $y_caption $y_tail / $f_head $* $f_tail"
full_width=$(identify $f_head | perl -ne 'print($1) if /^\S+ \w+ (\d+)/')
full_height=$(identify $f_head | perl -ne 'print($1) if /^\S+ \w+ \d+x(\d+)/')
width=$(( $full_width - 2*$x_pad ))
set -x
convert -crop "${width}x$(( $y_tail - $y_content ))+${x_pad}+${y_content}" $f_head /tmp/sscapcat0-${f_head##*/}
caption_height=$(( $y_tail - $y_caption ))
for f in $@ ; do convert -crop "${width}x${caption_height}+${x_pad}+${y_caption}" $f /tmp/sscapcat5-${f##*/} ; done
tail_height=$(( $full_height - $y_caption - 2 ))
convert -crop "${width}x${tail_height}+${x_pad}+${y_caption}" $f_tail /tmp/sscapcat9-${f_tail##*/}
ext="${f_head##*.}"
convert -append /tmp/sscapcat[059]-*.$ext $out_file
rm /tmp/sscapcat[059]-*.$ext
set +x
# identify 1.png
# => 1.png PNG 836x636 836x636+0+0 8-bit sRGB 358957B 0.000u 0:00.000
# convert -crop 800x430+18+75 1.png head.png
# for i in 2 3 ; do convert -crop 800x40+18+465 $i.png zz/pic/$i.png ; done
# convert -crop 800x171+18+465 4.png tail.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment