use dimensions from one image to resize another

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
mrmernan

use dimensions from one image to resize another

Post by mrmernan »

..trying to do resize img2 using img1's dimensions

i.e. kinda like; convert -resize img2.jpg img1.%w\ximg1.%h!

..tried setting the %w %h vars w/ -label, -size, etc, but cant get it to store them
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: use dimensions from one image to resize another

Post by fmw42 »

mrmernan wrote:..trying to do resize img2 using img1's dimensions

i.e. kinda like; convert -resize img2.jpg img1.%w\ximg1.%h!

..tried setting the %w %h vars w/ -label, -size, etc, but cant get it to store them
You can use two lines: get size, then use it:
size=`convert rose: -format "%wx%h" info:`
convert logo: -resize "$size" logo_rose.png

or combine them into one line:
convert logo: -resize "$(convert rose: -format "%wx%h" info:)" logo_rose.png

Examples above use rose: and logo: built-in images. So you can try it.
mrmernan

Re: use dimensions from one image to resize another

Post by mrmernan »

thanks! ..couldnt get your example to work via our fussy linux/tcsh environment (when used verbatim) , but your basic idea did inspire me to this;

convert infile2.jpg -resize `identify infile1.jpg -format "%wx%h!"` outfile.jpg

thanks again!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: use dimensions from one image to resize another

Post by anthony »

Eventually (probably with IM v7) you will be able to do this more directly, but for now you will need to use two commands.

An API interface, like PerlMagick, IMagikc, or Rmagick, can do this more directly.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: use dimensions from one image to resize another

Post by fmw42 »

mrmernan wrote:thanks! ..couldnt get your example to work via our fussy linux/tcsh environment (when used verbatim) , but your basic idea did inspire me to this;

convert infile2.jpg -resize `identify infile1.jpg -format "%wx%h!"` outfile.jpg

thanks again!

Yes, identify and convert can both be used to get string formats. So

convert infile2.jpg -resize `convert infile1.jpg -format "%wx%h!" info:` outfile.jpg

should work also.

On some systems, at least from what I can tell,

`...` is the same as $(...)

You may get some faster results adding -ping, so that the image does not have to be read in.

convert infile2.jpg -resize `identify -ping -format "%wx%h!" infile1.jpg` outfile.jpg

See

http://www.imagemagick.org/Usage/basics/#controls
Post Reply