Page 1 of 1
Looking for Command to Square an Image without Losing Information
Posted: 2017-04-10T08:00:46-07:00
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
Re: Looking for Command to Square an Image without Losing Information
Posted: 2017-04-10T08:22:06-07:00
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 "\( ... \)".
Re: Looking for Command to Square an Image without Losing Information
Posted: 2017-04-10T08:25:38-07:00
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!