summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcombine-images89
1 files changed, 89 insertions, 0 deletions
diff --git a/combine-images b/combine-images
new file mode 100755
index 0000000..e1b6e8c
--- /dev/null
+++ b/combine-images
@@ -0,0 +1,89 @@
+#!/bin/bash
+
+set -e
+
+tmp_dir="$(mktemp -d)"
+trap 'rm -rf --one-file-system "${tmp_dir}"' EXIT
+
+if [ $# -ne 3 ]; then
+ echo 'Verwendung: combine-images Geometrie Dateistamm_ Endung'
+ echo ' Geometrie hat die Form "Breite"x"Hoehe"+"x-Offset"+"y-Offset"'
+ exit 1
+fi
+
+geometry="$1"
+stem="$2"
+ending="$3"
+
+files="$(
+ ls "$(dirname "${stem}")" | \
+ grep "^$(basename "${stem}")[0-9]\+_[0-9]\+\.${ending}\$"
+)"
+
+numbers="$(
+ echo "${files}" | \
+ sed "
+ s|^$(basename "${stem}")||
+ s|\.${ending}\$||
+ " | \
+ sort
+)"
+
+min_x="$(
+ echo "${numbers}" | \
+ sed 's|_[0-9]\+$||' | \
+ sort -nr | \
+ tail -n1
+)"
+
+max_x="$(
+ echo "${numbers}" | \
+ sed 's|_[0-9]\+$||' | \
+ sort -n | \
+ tail -n1
+)"
+
+min_y="$(
+ echo "${numbers}" | \
+ sed 's|^[0-9]\+_||' | \
+ sort -nr | \
+ tail -n1
+)"
+
+max_y="$(
+ echo "${numbers}" | \
+ sed 's|^[0-9]\+_||' | \
+ sort -n | \
+ tail -n1
+)"
+
+if ! [ "${numbers}" = "$(
+ for ((x=${min_x}; x<=${max_x}; x++)); do
+ for ((y=${min_y}; y<=${max_y}; y++)); do
+ echo "${x}_${y}"
+ done
+ done
+ )" ]; then
+ echo 'Die angegebenen Bilder ergeben kein Rechteck!'
+ exit 1
+fi
+
+for ((x=${min_x}; x<=${max_x}; x++)); do
+ for ((y=${min_y}; y<=${max_y}; y++)); do
+ convert "${stem}${x}_${y}.${ending}" -crop "${geometry}" "${tmp_dir}/C${x}_${y}.bmp"
+ printf '.'
+ done
+ convert $(
+ for ((y=${max_y}; y>=${min_y}; y--)); do
+ printf '%s ' "${tmp_dir}/C${x}_${y}.bmp"
+ done
+ ) -append "${tmp_dir}/C${x}.bmp"
+ printf '.'
+done
+
+convert $(
+ for ((x=${min_x}; x<=${max_x}; x++)); do
+ printf '%s ' "${tmp_dir}/C${x}.bmp"
+ done
+) +append "${stem%_}.${ending}"
+printf '.\n'