Resizing canvas based on current height/width

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
MilOrg
Posts: 5
Joined: 2015-02-11T06:01:43-07:00
Authentication code: 6789

Resizing canvas based on current height/width

Post by MilOrg »

Hi all :-)

Can't seem to wrap my head around this one :roll: - hopefully one if you geniuses can :D

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! :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing canvas based on current height/width

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resizing canvas based on current height/width

Post by snibgo »

See also the recent thread with very similar problem: viewtopic.php?f=1&t=26963
snibgo's IM pages: im.snibgo.com
MilOrg
Posts: 5
Joined: 2015-02-11T06:01:43-07:00
Authentication code: 6789

Re: Resizing canvas based on current height/width

Post by MilOrg »

Thanks a million for the input, guys - much appreciated! :D
MilOrg
Posts: 5
Joined: 2015-02-11T06:01:43-07:00
Authentication code: 6789

Re: Resizing canvas based on current height/width

Post 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! :D
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resizing canvas based on current height/width

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing canvas based on current height/width

Post 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
MilOrg
Posts: 5
Joined: 2015-02-11T06:01:43-07:00
Authentication code: 6789

Re: Resizing canvas based on current height/width

Post by MilOrg »

Will do, fmw42 - thank you so much, very appreciated in deed! :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing canvas based on current height/width

Post by fmw42 »

Sorry, I was testing. So replace show: with your actual output file name
MilOrg
Posts: 5
Joined: 2015-02-11T06:01:43-07:00
Authentication code: 6789

Re: Resizing canvas based on current height/width

Post by MilOrg »

Ah ok - thanks, not very familiar with the syntax yet so would probably have scratched my head a few times with that :wink:
Post Reply