Skip to content

Instantly share code, notes, and snippets.

@orllewin
Last active February 27, 2022 18:06
Show Gist options
  • Select an option

  • Save orllewin/a88bc22232a2324ca6a47cb8b6ec1f11 to your computer and use it in GitHub Desktop.

Select an option

Save orllewin/a88bc22232a2324ca6a47cb8b6ec1f11 to your computer and use it in GitHub Desktop.
Stretches images horizontally by the given scale factor, used to correct images taken with an anamorphic lens
#!/bin/bash
echo -e "\n\tIMGWIDEN\n\tAnamorphic photo utility\n"
# Check ImageMagick Identify is available
if ! command -v identify &> /dev/null
then
echo -e "'identify' could not be found, make sure you have ImageMagick fully installed\n"
exit
fi
# Check filename argument
if [ "$1" ]; then
echo "Input file: $1"
else
echo -e "\nMissing input file.\n\n\tUsage: imgwiden filename horizontalScaleFactor\n\n\teg. imgwiden photo.jpg 1.33\n"
exit
fi
# Check scale factor argument
if [ "$2" ]; then
echo "Horizontal scale factor: $2"
else
echo -e "\nMissing scale factor.\n\n\tUsage: imgwiden filename horizontalScaleFactor\n\n\teg. imgwiden photo.jpg 1.33\n"
exit
fi
# Read image dimensions
ident="$(identify $1)"
imgDimens="$(cut -d' ' -f3 <<<"$ident")"
echo -e "Image dimensions: $imgDimens"
width="$(cut -d'x' -f1 <<<"$imgDimens")"
height="$(cut -d'x' -f2 <<<"$imgDimens")"
echo -e "Width:\t\t$width"
# Calculate target width
scaledWidth=$(echo $width \* $2 | bc -l)
echo -e "Scaled width:\t$scaledWidth"
# Check ImageMagick Convert is available
if ! command -v convert &> /dev/null
then
echo -e "'convert' could not be found, make sure you have ImageMagick fully installed\n"
exit
fi
# Stretch image using ImageMagick
filename="${1%.*}"
extension="${1##*.}"
outputFilename="${filename}_x${2}.$extension"
echo -e "Output filename: $outputFilename"
`convert $1 -resize ${scaledWidth}x${height}\! $outputFilename`
# MacOS only, open in Preview
open $outputFilename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment