calculate resize dimensions and stop

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
kitchin
Posts: 7
Joined: 2011-06-26T09:04:32-07:00
Authentication code: 8675308

calculate resize dimensions and stop

Post by kitchin »

Before I resize an image, I'd like to get the new dimensions. Rather than write code to calculate the new dimensions, can I just get Imagemagick to do it? Something like

Code: Select all

convert foo.png -resize "100x100>" -identify /dev/null
but without processing the resize.

keywords: trial run, test
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: calculate resize dimensions and stop

Post by fmw42 »

Whether you throw away the result or not, IM will process the image with your example above.

This will get the information, but will do the resize and throw away the image.

Code: Select all

convert image -resize "100x100>" -verbose -write info: null:

What information specifically are you looking for? Depending upon what you want there may be other ways. If you just want new size information, that can can be computed with some fx: computations without actually resizing.
kitchin
Posts: 7
Joined: 2011-06-26T09:04:32-07:00
Authentication code: 8675308

Re: calculate resize dimensions and stop

Post by kitchin »

Yeah, that's what I'm thinking. I'd like to avoid reproducing the logic that does "-resize 100x100>", though I guess it's a standard enough algorithm. (Though not standard enough that Wordpress does it right! Until recently WP was often off-by-one on the rounding.)

I'm looking for the new width and height.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: calculate resize dimensions and stop

Post by fmw42 »

This is Unix syntax. This will do it if you are using Linux, Mac OSX or Windows with Cygwin. If on plain Windows, another user will need to help as I do not have a PC or use Windows and the syntax for scripting is quite different.

Change the infile to whatever image you want rather than the IM internal logo: image and set tw and th to whatever resize values you want. The following script takes into account the equivalent of the > in the resize.

Code: Select all

infile="logo:"
ww=`convert "$infile" -format "%w" info:`
hh=`convert "$infile" -format "%h" info:`
tw=100
th=100
aspect=`convert xc: -format "%[fx:$ww/$hh]" info:`
mode=`convert xc: -format "%[fx:$aspect>=1?1:0]" info:`
if [ $mode -eq 1 ]; then
width=`convert xc: -format "%[fx:round(($ww>$tw || $hh>$th)?$tw:$ww)]" info:`
height=`convert xc: -format "%[fx:round(($ww>$tw || $hh>$th)?$tw/$aspect:$hh)]" info:`
else
width=`convert xc: -format "%[fx:round(($ww>$tw || $hh>$th)?$tw*$aspect:$ww)]" info:`
height=`convert xc: -format "%[fx:round(($ww>$tw || $hh>$th)?$tw:$hh)]" info:`
fi
echo "width=$width; height=$height;"
kitchin
Posts: 7
Joined: 2011-06-26T09:04:32-07:00
Authentication code: 8675308

Re: calculate resize dimensions and stop

Post by kitchin »

Thanks, that looks great and must match what IM does internally. If it misses on a corner case, no big deal, I've still saved processing time in the other 99% of cases.
Post Reply