Skip to content

Instantly share code, notes, and snippets.

@theodric
Created February 19, 2026 16:33
Show Gist options
  • Select an option

  • Save theodric/d73f25fa1492fe77ef44973fd193f56c to your computer and use it in GitHub Desktop.

Select an option

Save theodric/d73f25fa1492fe77ef44973fd193f56c to your computer and use it in GitHub Desktop.
pad a directory of images to 1:1 aspect ratio (overwriting original files)
#!/bin/bash
# use `-format png` to change output format, converting files at the same time, but then perhaps consider changing the final $img in line 12 to somethingelse_$img
for img in *.jpg *.jpeg *.png; do
if [ -f "$img" ]; then
width=$(identify -format "%w" "$img" 2>/dev/null)
height=$(identify -format "%h" "$img" 2>/dev/null)
if [ -z "$width" ] || [ -z "$height" ]; then
echo "Skipping $img (not a valid image)"
continue
fi
size=$((width > height ? width : height))
convert "$img" -gravity center -background white -extent "${size}x${size}" "$img"
echo "Padded $img to ${size}x${size}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment