Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active November 13, 2022 10:29
Show Gist options
  • Save oguz-ismail/b8b06e38941c520c5bbdb90bc6b25211 to your computer and use it in GitHub Desktop.
Save oguz-ismail/b8b06e38941c520c5bbdb90bc6b25211 to your computer and use it in GitHub Desktop.
paste images side by side
err_usage() {
printf 'Usage: %s [-c columns] [-o outfile] image image...\n' "$0" >&2
exit 1
}
columns=0
out_file=images.jpg
while getopts :c:o: opt; do
case $opt in
(c)
case $OPTARG in
(''|*[!0-9]*)
err_usage
esac
columns=$OPTARG ;;
(o)
out_file=$OPTARG ;;
(*)
err_usage
esac
done
shift $((OPTIND - 1))
if test $# -lt 2; then
err_usage
fi
get_size() {
ffprobe -loglevel error \
-show_entries stream=width,height \
-of compact=nk=1:p=0:s=x \
"$1"
}
end_of_row() {
if test $max_width -lt $sum_widths; then
max_width=$sum_widths
fi
sum_heights=$((sum_heights + max_height))
offset_y=$((offset_y + max_height))
offset_x=0
sum_widths=0
max_height=0
}
sum_widths=0 sum_heights=0
max_height=0 max_width=0
offset_x=0 offset_y=0
img_index=0 column=0
filter=
for img_path; do
img_size=$(get_size "$img_path")
img_width=${img_size%x*}
img_height=${img_size#*x}
if test $max_height -lt $img_height; then
max_height=$img_height
fi
sum_widths=$((sum_widths + img_width))
shift
set -- "$@" -i "$img_path"
filter=$filter[bg$img_index],[bg$img_index][$img_index]overlay=$offset_x:$offset_y
column=$((column + 1))
if test $column -eq $columns; then
end_of_row
column=0
else
offset_x=$((offset_x + img_width))
fi
img_index=$((img_index + 1))
done
end_of_row
filter="color=s=${max_width}x$sum_heights$filter,trim=end_frame=1"
ffmpeg -loglevel error \
"$@" \
-filter_complex "$filter" \
"$out_file"
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment