Page 1 of 1
Resizing canvas based on current height/width
Posted: 2015-02-11T06:25:16-07:00
by MilOrg
Hi all
Can't seem to wrap my head around this one

- hopefully one if you geniuses can
I have a bunch of images with varying sizes and aspect ratios where I need to add white canvas so they end up with an aspect ratio of 4/3.
Example 1: If the image is 600 x 300 pixels, I need for IM to add a white canvas to the top and bottom so it ends up 600 x 450 pixels.
Example 2: If the image is 600 x 510 pixels, I need for IM to add a white canvas to the left and right so it ends up 680 x 510 pixels.
Thanks a gazillion in advance for any help!

Re: Resizing canvas based on current height/width
Posted: 2015-02-11T11:09:36-07:00
by fmw42
You will need to get the image width and height and aspect and then compute the desired aspect and width and height for the output. Then just use -background white -extent WxH where WxH is the final desired size. I know of no way to do this automatically with one IM command.
I have a unix script called aspectpad, the will pad out to some desired aspect ratio. See my link below.
Re: Resizing canvas based on current height/width
Posted: 2015-02-11T11:33:47-07:00
by snibgo
See also the recent thread with very similar problem:
viewtopic.php?f=1&t=26963
Re: Resizing canvas based on current height/width
Posted: 2015-02-11T14:01:32-07:00
by MilOrg
Thanks a million for the input, guys - much appreciated!

Re: Resizing canvas based on current height/width
Posted: 2015-02-16T09:56:42-07:00
by MilOrg
Still struggling a bit with this one - is there any way to have IM detect the image width and height and then compute the correct adjusted width or height based on those values + aspect ratio? Or any workarounds to accomplish the same result...?
/me is stumped
Any help would be greatly appreciated!

Re: Resizing canvas based on current height/width
Posted: 2015-02-16T11:31:27-07:00
by Bonzo
No I do not think so or else it would have been suggested.
You could have helped yourself by giving more information in your question; mainly how you are running Imagemagick and what version you are using.
Re: Resizing canvas based on current height/width
Posted: 2015-02-16T12:45:37-07:00
by fmw42
See my script aspectpad for the concepts. Then you can either use it or write it again for our platform or API. IM does not have a built-in function to do just what you want.
Try this if on unix (Linux, Mac OS, Windows with Cygwin)
Code: Select all
infile="yourfile"
bgcolor=white
ww=`convert $infile -format "%w" info:`
hh=`convert $infile -format "%h" info:`
ratio=`echo "scale=6; 4/3" | bc`
aspect=`convert xc: -format "%[fx:$ww/$hh]" info:`
test=`convert xc: -format "%[fx:$aspect<$ratio?1:0]" info:`
if [ $test -eq 1 ]; then
www=`convert xc: -format "%[fx:$ww*$ratio/$aspect]" info:`
convert $infile -gravity center -background $bgcolor -extent ${www}x${hh} show:
else
hhh=`convert xc: -format "%[fx:$hh*$aspect/$ratio]" info:`
convert $infile -gravity center -background $bgcolor -extent ${ww}x${hhh} show:
fi
Re: Resizing canvas based on current height/width
Posted: 2015-02-16T12:48:05-07:00
by MilOrg
Will do, fmw42 - thank you so much, very appreciated in deed!

Re: Resizing canvas based on current height/width
Posted: 2015-02-16T12:52:56-07:00
by fmw42
Sorry, I was testing. So replace show: with your actual output file name
Re: Resizing canvas based on current height/width
Posted: 2015-02-16T12:55:13-07:00
by MilOrg
Ah ok - thanks, not very familiar with the syntax yet so would probably have scratched my head a few times with that
