Page 1 of 1
calculate resize dimensions and stop
Posted: 2015-01-24T04:30:44-07:00
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
Re: calculate resize dimensions and stop
Posted: 2015-01-24T12:21:47-07:00
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.
Re: calculate resize dimensions and stop
Posted: 2015-01-24T20:40:25-07:00
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.
Re: calculate resize dimensions and stop
Posted: 2015-01-24T21:03:59-07:00
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;"
Re: calculate resize dimensions and stop
Posted: 2015-01-26T23:25:41-07:00
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.