Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Created January 28, 2023 00:16
Show Gist options
  • Save tylerneylon/129cfe6288af018aa0ed7134a906975a to your computer and use it in GitHub Desktop.
Save tylerneylon/129cfe6288af018aa0ed7134a906975a to your computer and use it in GitHub Desktop.
A little bash script to make your images 16x9 via adding white border stripes.
#!/bin/bash
if [ -z "$1" ]; then
echo Usage: ./fix_aspect.sh '<img_file>'
echo
echo This will create a new image file with the word 'fixed' appended before
echo the filename extension. Eg, myimg.jpg will become myimg.fixed.jpg.
exit
fi
resolution=$(identify $1 | awk '{print $3}')
orig_w=$(echo $resolution | cut -f1 -dx)
orig_h=$(echo $resolution | cut -f2 -dx)
new_h=$(python3 -c 'print('$orig_w' * 9 // 16)')
new_w=$(python3 -c 'print('$orig_h' * 16 // 9)')
output_stem=${1%%.*}
output_ext=${1##*.}
output_fname=${output_stem}.fixed.${output_ext}
if [ $new_h -lt $orig_h ]; then
# Use new_w to increase the width.
convert $1 -gravity center -extent ${new_w}x${orig_h} $output_fname
else
# Use new_h to increase the height.
convert $1 -gravity center -extent ${orig_w}x${new_h} $output_fname
fi
echo Fixed image saved to the file: $output_fname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment