Batch Resize Canvas Size to Longest Side

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
FUrn
Posts: 20
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Batch Resize Canvas Size to Longest Side

Post by FUrn »

I have a number of rectangular images (with different orientation) that I'd like to convert into square ones. I don't have specific dimensions for the square images, but they should be the length of the longest side of the original image, e.g:

188x166 -> 188x188
1200x1600 -> 1600x1600

I'd imagine I need to adjust the canvas to this new size (using white background), and ensure the original image is centered in the new canvas. Is this possible on ImageMagick?

I've come across the following code, but obviously need to find a way to replace the '640x640' with whatever the longest dimension of the image in question is:

Code: Select all

mogrify -extent 640x640 -gravity Center -fill white *.jpg
Thank you
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch Resize Canvas Size to Longest Side

Post by fmw42 »

What is your version of ImageMagick and platform? IM 6 and IM 7 have different syntax. But you will likely need to use convert/magick depending upon version rather than mogrify. You may need to write a script to loop over each image in your directory and that is OS dependent. For IM 7 you might be able to do multiple images in a single magick command, if you have enough memory and do not mind output name changes.


__________

See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at http://www.imagemagick.org/discourse-se ... f=1&t=9620

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch Resize Canvas Size to Longest Side

Post by snibgo »

With v7, you can do individual files like this...

Code: Select all

magick in.png -gravity Center -background Blue -extent %[fx:max(w,h)]x%[fx:max(w,h)] out.png
... then put that in a shell loop.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Batch Resize Canvas Size to Longest Side

Post by GeeMack »

FUrn wrote: 2018-10-30T09:53:59-07:00I'd imagine I need to adjust the canvas to this new size (using white background), and ensure the original image is centered in the new canvas. Is this possible on ImageMagick?
This command will take any input image and pad it with white as necessary to create a square output image. If the input is wider than high, it will pad the top and bottom while centering the input in the square. If it's higher than wide, it will pad the left and right so the input is centered in the square output. If the input is already square it will leave the dimensions unchanged.

Code: Select all

convert input.png -virtual-pixel white ^
   -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]" ^
   -distort affine "0,0 %[fx:w<h?(h-w)/2:0],%[fx:w>h?(w-h)/2:0]" result.png
That uses IM6 and is in Windows syntax. For IM7 use "magick" instead of "convert". To put it in a Windows BAT script, double the percent signs "%%". To run it as a *nix shell command, replace those end-of-line carets "^" with backslashes "\".

Note that if you're using IM version 7, the example command snibgo provided above is a much more straightforward approach.

Either way, to perform this sort of operation on multiple files it's probably best to use a "for" loop as snibgo suggested. The syntax depends on the platform you're running on.
FUrn
Posts: 20
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Re: Batch Resize Canvas Size to Longest Side

Post by FUrn »

snibgo wrote: 2018-10-30T12:10:39-07:00 With v7, you can do individual files like this...

Code: Select all

magick in.png -gravity Center -background Blue -extent %[fx:max(w,h)]x%[fx:max(w,h)] out.png
... then put that in a shell loop.
Thanks snibgo - this is helpful. There was a little syntax error when I ran the code, but adding quotation marks helped:

Code: Select all

magick in.jpg -gravity Center -background White -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" out.jpg
I'm very much a beginner with IM and non-GUI editors though, so I hope it isn't too much to ask how to put that in a shell loop?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch Resize Canvas Size to Longest Side

Post by snibgo »

How can we, if you don't tell us what shell you are using?

For Windows, use the "for" command.

For bash, use the "for" command (but bash "for" is different to Windows "for").
snibgo's IM pages: im.snibgo.com
FUrn
Posts: 20
Joined: 2018-10-30T09:49:10-07:00
Authentication code: 1152

Re: Batch Resize Canvas Size to Longest Side

Post by FUrn »

Got it:

Code: Select all

for filename in *.jpg; do
    magick "$filename" -gravity Center -background White -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" "converted-$filename"
done
Thanks for all your help in getting me here...it works perfectly now!
Post Reply