Created
February 19, 2026 16:33
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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