Page 1 of 1

Dynamic resizing

Posted: 2010-09-29T13:51:25-07:00
by asimmons
I'm trying to resize an image based of the size of another image. So basically I have a the large image and need it to look at its smaller version and change its size to the smaller ones size. I know that the resize option can take parameters as the resize values but I don't know how to get the w and h of the smaller image while trying to resize the larger version.

So basically I want to look at a an image get its size and use it to resize the bigger version and since this will be done in a batch I need it to be at the same time. I was thinking something like this.

identify -format %%w%%h small/image.png (+convert big.png -resize %%wx%%h small/image.png)

Any help would be greatly appreciated. Also this is to port a image to a smaller resolution mobile device.

Re: Dynamic resizing

Posted: 2010-09-29T14:02:38-07:00
by fmw42
You will have to do that with multiple commands. Get the size of the smaller image, then use that to resize the larger image.

smallsize=`convert smallimage -format "%wx%h" info:`
convert largeimage -resize ${smallsize}! reducedimage

Note the ! will force the large image to have exactly the same size as the smaller one even if the aspect is changed. See http://www.imagemagick.org/script/comma ... p#geometry for more details on resizing arguments.

This will work to put it all in one command line, but it is really the same thing:


convert largeimage -resize $(convert smallimage -format "%wx%h" info:) reducedimage

This is all for unix. I don't know how one would do that on Windows. But for Windows help see http://www.imagemagick.org/Usage/windows/

Re: Dynamic resizing

Posted: 2010-09-29T14:29:12-07:00
by asimmons
freaking awesome man... thats what I wanted thank you very much. I had it working but in a much more round about way. I'll post some results later

Re: Dynamic resizing

Posted: 2010-09-29T20:40:03-07:00
by anthony
The only way to do this without using two commands is to use the more general -distort to resize the image. This accepts percent escapes in its arguments.

However even this is not enough as it will only have access to the source images attributes, and not image attributes from another image.

For that you will need to use a separate global -set option grab the attributes from the other image.

For an example of this see...
http://www.imagemagick.org/Usage/transform/#fx_other

This is actually a miss-placed example and will eventually move to 'Basics' area of Image Attributes, Metadata and Settings. At least when I get the bug to finally finish writing that section.

So to say resize a logo: image the same size as a rose: image without previous knowledge of the size of the rose: image you can do this...

Code: Select all

   convert rose: -set option:rw %w -set option:rh %h +delete \
               logo: -alpha on -virtual-pixel transparent \
               +distort Affine '0,0 0,0     %w,0 %[rw],0   0,%h  0,%[rh]' \
               logo_sized_as_rose.png
Note distort will generate slightly larger 'layered image' on a virtual canvas. It wil have fuzzy edges too. This is because it is generating an exact or true distortion of the image, and not a sanitised orthogonal Resized image.

Here is another version that uses the original image to trim and crop the virtual pixels of the distorted image..

Code: Select all

   convert rose: -set option:rw %w -set option:rh %h  \
               \( logo: +distort Affine '0,0 0,0     %w,0 %[rw],0   0,%h  0,%[rh]' \) \
               -flatten  logo_overlaid_on_rose.png
This has been added to Image Distorts, in the section "Control Points using Percent Escapes"
http://www.imagemagick.org/Usage/distor ... ol_escapes

Re: Dynamic resizing

Posted: 2010-09-29T21:36:09-07:00
by anthony
It would be nice is -resize could accept percent arguments, but before that can happen some method of resolving normal 'percent' useage, and 'percent escape' usage needs to be resolved in a acceptable manner. If it can, percent escapes could become even more common!