Resize all images to the same dimension while preserving the aspect ratio

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
menteith
Posts: 10
Joined: 2015-03-30T14:49:19-07:00
Authentication code: 6789

Resize all images to the same dimension while preserving the aspect ratio

Post by menteith »

Hi all,

My questions is quite unusual but it's very important for me.

I have a bunch of files with one of the dimensions (either heigh or width) the same while the other is of very similar value to each other (e.g. all have heigh of 2576 and the width is sometimes 2800, sometimes 2822 etc.). The task would be to resize (in fact shrink using e.g. Only Shrink Larger Images ('>' flag)) all files to one dimension while preserving the aspect ratio. I am not sure, but it perhaps could be done by shrinking each image based on the largest fitting dimension. My second guess is to make each image same size by adding white boarders and then crop. What do you think?

Perhaps I am a bit closer to the solution. I checked this post: viewtopic.php?p=71482#p71482

and wrote a command

Code: Select all

convert in.jpg -resize 4000x3000^ -gravity center -background white -extent 4000x3000 out.jpg
As far as I can tell, this command resizes (using white background) file in.jpg and saves it to out.jpg? Am I right?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize all images to the same dimension while preserving the aspect ratio

Post by fmw42 »

The command resizes the smaller of the two image dimensions compared to your specified dimensions using ^ so that the result will be one dimension having the specified size and other larger than the specified size. Then the extent crops to 4000x3000.

The alternate would be

Code: Select all

convert in.jpg -resize 4000x3000 -gravity center -background white -extent 4000x3000 out.jpg
This will resize to the larger dimension, so that one is the right size and the other smaller than the desired size. The extent will then pad with white to fill out to the desired size.

see
http://www.imagemagick.org/Usage/thumbnails/#pad
http://www.imagemagick.org/Usage/thumbnails/#cut
menteith
Posts: 10
Joined: 2015-03-30T14:49:19-07:00
Authentication code: 6789

Re: Resize all images to the same dimension while preserving the aspect ratio

Post by menteith »

Thank you! I have used the last command and when it has finished I used the following (in a .bat file in windows) to split images into two:

Code: Select all

set "parameters=-crop 2x1@ +repage -colorspace gray +dither -colors 4"
FOR %%f IN (*.jpg) DO convert "%%f" %parameters% "split\%%~nf_%%d.jpg"
Surprisingly, not all images have the same dimensions. Why? How it can be fixed?
Post Reply