Blog-Archiv

Dienstag, 22. März 2022

Scale Images to Same Size with ImageMagick

Scaling a number of images to same size means:

  1. specifying a dimension like 1920x1080 pixels (currently the standard video dimension)
  2. sizing all images inside a directory to that size
  3. while keeping their aspect ratio

This is possible only by adding edges when some width or height is too small to fit the aspect ratio. In other words, every circle in an image will still be a circle afterwards (not an ellipse), but it may have colored edges on top and bottom, or at left and right, symmetrically.

Here is how to do that.

Prerequisites

You must have a UNIX shell script execution environment. Under WINDOWS you can install CygWin. Under LINUX it's normally already integrated into the operating system.

Install ImageMagick on your machine. It is available for many platforms.

Script Fragment

Put following shell script into the directory where your images are, and run it. The script assumes the images are *.png, adjust line 1 when not. Further it sizes to 1920x1080 pixels (a standard dimension), adjust line 8 when you want a different size. You can change the color of the added edges through the "-background colorOfYourChoice" option.

extension=png

for imageFile in *.$extension
do
    sized=`basename "\$imageFile" .\$extension`_sized.$extension
    echo "Scaling $imageFile to $sized ..." >&2
	
    convert "$imageFile" -resize 1920x1080 -gravity center -background black -extent 1920x1080 "$sized"
done

echo "Done." >&2

After execution of the script you will have additional images in the directory, carrying the "_sized" postfix in their names. These are the result images.

Reusable Script

Here is a shell script that sizes one image to an optionally given dimension. You define the path of the target file as command line argument, and, optionally, also the target dimension. The script will check its parameters and output its syntax when called incorrectly:

  • imageToSize.sh
####################################################
# Scales an image to a fixed size using ImageMagick.
####################################################

defaultDimension=1920x1080	# standard video dimension
edgeColor=black

syntax()	{
	echo "SYNTAX: $0 sourceImageFile targetImageFile [widthxheight]" >&2
	echo "	Scales an image to width x height, keeping aspect ratio, adding edges." >&2
	echo "	sourceImageFile: the image to scale" >&2
	echo "	targetImageFile: the result image file" >&2
	echo "	widthxheight: optional target dimension, default is $defaultDimension" >&2
	exit 1
}

[ -f "$1" -a -n "$2" ] || syntax
sourceImageFile="$1"
targetImageFile="$2"
dimension=${3:-$defaultDimension}	

echo "Scaling to $dimension from $sourceImageFile to $targetImageFile ..." >&2

# call ImageMagick
convert "$sourceImageFile" \
	-resize $dimension \
	-gravity center \
	-background $edgeColor \
	-extent $dimension \
	"$targetImageFile"

Lots of lines around just one command line that does the real work. But that's how sustainable software is written.

Resume

How long have I been looking for that technique. How short is this solution. Thank you ImageMagick!




Keine Kommentare: