Last active
July 15, 2024 17:24
-
-
Save rahji/28a272b26daf8fa8a78045c979e462bd to your computer and use it in GitHub Desktop.
Use ImageMagick to make a landscape image square
This file contains 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 | |
# 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