Skip to content

Instantly share code, notes, and snippets.

@dhcgn
Last active June 19, 2025 20:24
Show Gist options
  • Save dhcgn/b7f08bb52ea9ffd71ff385ef3f17da06 to your computer and use it in GitHub Desktop.
Save dhcgn/b7f08bb52ea9ffd71ff385ef3f17da06 to your computer and use it in GitHub Desktop.
This script creates A4 (DIN A1) collage pages from any number of PNG images, automatically resizing and arranging them into printable pages using ImageMagick.
#!/bin/bash
# -----------------------------------------------------------------------------
# Script: convert.sh
#
# Description:
# This script processes all PNG images in the specified source folder by:
# 1. Squaring and resizing them to 1000x1000 pixels (centered, white background).
# 2. Arranging them into A4-sized (2480x3508 px, 300 DPI) collage pages,
# with 3 images per row and 4 rows per page (12 images per page).
# 3. Saving the resulting collages in the 'result' folder.
#
# Usage:
# ./convert.sh
#
# Requirements:
# - ImageMagick (convert, montage)
# - Bash shell
#
# Output:
# - Resulting collage pages are saved as result/page_1.png, result/page_2.png, ...
#
# Folder structure:
# - Source images: REPLACE_FOLDER_NAME/
# - Output: REPLACE_FOLDER_NAME/result/
#
# -----------------------------------------------------------------------------
# Cleanup previous results
RESULTFOLDER="REPLACE_FOLDER_NAME/result"
SQUAREFOLDER="$RESULTFOLDER/squared"
rm -rf "$RESULTFOLDER"
mkdir -p "$SQUAREFOLDER"
# Folders
SRCFOLDER="REPLACE_FOLDER_NAME/"
# --- 1. Square all PNG images (centered, 1000x1000 px for processing, keep ratio) ---
echo "Squaring images..."
find "$SRCFOLDER" -maxdepth 1 -iname '*.png' -print0 | while IFS= read -r -d '' f; do
b=$(basename "$f")
convert "$f" -resize 1000x1000 -background white -gravity center -extent 1000x1000 "$SQUAREFOLDER/$b"
done
# --- 2. Montage settings for A4 portrait at 300 DPI ---
IMGS_PER_ROW=3
ROWS_PER_PAGE=4
IMGS_PER_PAGE=$((IMGS_PER_ROW*ROWS_PER_PAGE)) # 12
# Final A4 size in pixels
WIDTH=2480
HEIGHT=3508
# --- 3. Create collages ---
cd "$SQUAREFOLDER"
mapfile -d '' files < <(find . -maxdepth 1 -iname '*.png' -print0 | sort -z)
total_images=${#files[@]}
num_pages=$(( (total_images + IMGS_PER_PAGE - 1) / IMGS_PER_PAGE ))
echo "Creating $num_pages A4 collage pages..."
for ((page=1; page<=num_pages; page++)); do
imgs=()
for ((i=0; i<IMGS_PER_PAGE; i++)); do
idx=$(( (page-1)*IMGS_PER_PAGE + i ))
[ $idx -ge $total_images ] && break
# Remove leading ./ from filenames for montage
img="${files[$idx]}"
img="${img#./}"
imgs+=("$img")
done
# Create the montage without -extent
montage "${imgs[@]}" \
-tile ${IMGS_PER_ROW}x${ROWS_PER_PAGE} \
-geometry 827x827+0+0\! \
-background white \
"montage_tmp.png"
# Center and extend/crop to exact A4 size at 300 DPI
convert "montage_tmp.png" -gravity center -background white -extent ${WIDTH}x${HEIGHT} -density 300 "$RESULTFOLDER/page_${page}.png"
rm -f "montage_tmp.png"
echo "Created page_${page}.png in result folder"
if [ -f "$RESULTFOLDER/page_${page}.png" ]; then
echo "$RESULTFOLDER/page_${page}.png"
else
echo "Warning: $RESULTFOLDER/page_${page}.png was not created!"
fi
done
echo "All done! Collages saved as result/page_1.png ... result/page_${num_pages}.png"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment