Skip to content

Instantly share code, notes, and snippets.

@rahji
Last active July 15, 2024 17:24
Show Gist options
  • Save rahji/28a272b26daf8fa8a78045c979e462bd to your computer and use it in GitHub Desktop.
Save rahji/28a272b26daf8fa8a78045c979e462bd to your computer and use it in GitHub Desktop.
Use ImageMagick to make a landscape image square
#!/bin/bash
# Check if the correct number of arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 input_file output_file bar_color"
exit 1
fi
# Input and output filenames and bar color
input_file="$1"
output_file="$2"
bar_color="$3"
# Get the dimensions of the input image
dimensions=$(identify -format "%wx%h" "$input_file")
width=$(echo $dimensions | cut -d'x' -f1)
height=$(echo $dimensions | cut -d'x' -f2)
# Determine the size of the square canvas
if [ $width -ge $height ]; then
canvas_size=$width
else
canvas_size=$height
fi
# Create the square image with bars
convert "$input_file" -background "$bar_color" -gravity center -extent ${canvas_size}x${canvas_size} "$output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment