Home > database >  Imagemagick: How to bulk convert images into square
Imagemagick: How to bulk convert images into square

Time:02-15

I want the size of the square should be equal to the largest side of the original photo

E.g.

Original: 500 x 400 => Output:500 x 500 Original: 400 x 600 => Output: 600 x 600

CodePudding user response:

You can do that as follows for any given image in Imagemagick 7. To do multiple image, you would need to write a "for" loop, whose syntax depends upon your OS.

magick image.suffix -gravity center -background black -extent "%wx%h^" result.suffix

or

magick image.suffix -gravity center -background black -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" result.suffix

CodePudding user response:

Here is a better way to convert all images in a folder with either Imagemagick 6 or 7. I use -distort SRT's viewport to do the padding with a no-op warping (i.e. no rotation, scale or translation) in mogrify to process all images in the folder.

First create a new folder to hold your output images, if desired and use the -path option to mogrify. Specify a background color (in this case I use red).

For Imagemagick 7:

magick mogrify -path path_to/new_folder -virtual-pixel background -background red -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:0.5*(max(w,h)-w)]-%[fx:0.5*(max(w,h)-h)]"  distort srt 0 *

For Imagemagick 6, remove "magick" before mogrify.

  • Related