Looking for Command to Square an Image without Losing Information

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
zerp
Posts: 2
Joined: 2017-04-10T07:41:43-07:00
Authentication code: 1151

Looking for Command to Square an Image without Losing Information

Post by zerp »

I'm a new user with a photoshop and matlab background. Looking for a imagemagick command to take a image of unknown size and to add white area to it, in order to make the image square.

For example say a user gives me a 1600x900 image or maybe it's 900x1600; I want to add white area to make the final image 1600x1600 while keeping the same pixels in the center.

In photoshop I could issue an expand canvas command with a center option, in matlab I could copy smaller array onto a larger array.

How do you do it in ImageMagick?

Thank you :D
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Looking for Command to Square an Image without Losing Information

Post by GeeMack »

zerp wrote: 2017-04-10T08:00:46-07:00I'm a new user with a photoshop and matlab background. Looking for a imagemagick command to take a image of unknown size and to add white area to it, in order to make the image square.

For example say a user gives me a 1600x900 image or maybe it's 900x1600; I want to add white area to make the final image 1600x1600 while keeping the same pixels in the center
It's always more helpful if you let us know which version of ImageMagick you're using and which platform or OS you're working on. Your result would be pretty simple to achieve with either IM6 or IM7, but they would be quite different commands.

Using IM6 from a Windows command prompt, this would do what you want...

Code: Select all

convert input.png -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]" ^
   ( -clone 0 -distort SRT 0 -fill white -colorize 100 ) +swap -gravity center -composite output.png
You'd get the same result more directly using IM7 with this command...

Code: Select all

magick input.png -gravity center -background white -extent "%[fx:max(w,h)]x%[fx:max(w,h)]" output.png
If you're running it from a *nix shell you'd have to change those continued line carets "^" to backslashes "\", and you'd have to escape the parentheses around the clone operation with backslashes "\( ... \)".
zerp
Posts: 2
Joined: 2017-04-10T07:41:43-07:00
Authentication code: 1151

Re: Looking for Command to Square an Image without Losing Information

Post by zerp »

I am using ImageMagick-7.0.5-4-portable-Q16-x86

It would have taken me three days to figure out that command.

Thank you!
Post Reply