How to get image size?

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
wc2005
Posts: 6
Joined: 2011-04-26T01:44:31-07:00
Authentication code: 8675308

How to get image size?

Post by wc2005 »

Hello, everybody!
I am new to ImageMagick. I am using ImageMagick-6.6.9-Q16-windows on Windows XP. I want to get the size of the image which is previously resized for later use, how can I do that? Please help, thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get image size?

Post by fmw42 »

see string formats at http://www.imagemagick.org/script/escape.php

convert image -format "%w x %h" info:

gives the image width and height.

You can add -ping to make it faster by not reading the whole image

convert image -ping -format "%w x %h" info:
wc2005
Posts: 6
Joined: 2011-04-26T01:44:31-07:00
Authentication code: 8675308

Re: How to get image size?

Post by wc2005 »

Thanks for your help.
Your code works but please look at my .bat file below (the code creates reflection effect for an image):

Code: Select all

set s=960x1280
convert  "c:\h.jpg" -alpha on -background transparent -gravity South -extent %s% ( +clone -flip ( -size %s% gradient:"rgba(0,0,0,0.6)-none" -size 10x0 xc:none  -append ) -compose Dst_In -composite ( -size %s% gradient:black-white ) -compose Blur -set option:compose:args 2x2 -composite ) -append  "c:\a.png"
When I want to create reflection effect for an image, I have to see its properties to get its width and height to assign to variable s as the code above. Now, I want ImageMagick to get width and height of the image automatically but the code below does not work:

Code: Select all

convert  "c:\h.jpg" -alpha on -background transparent -gravity South -extent %%wx%%h ( +clone -flip ( -size %%wx%%h gradient:"rgba(0,0,0,0.6)-none" -size 10x0 xc:none  -append ) -compose Dst_In -composite ( -size %%wx%%h gradient:black-white ) -compose Blur -set option:compose:args 2x2 -composite ) -append  "c:\a.png"
Please help me, thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to get image size?

Post by fmw42 »

Sorry I am not a Windows user nor do I know Bat scripting. When you say it does not work, that does not help as you don't say how it does not work. So I would suggest you place -write tmp1.png and -write tmp2.png etc in various places to track your different complex steps. Then you can find out where things are going wrong.

Note also you cannot get width and height calculated in-line with %w and %h in specifying -size, such as -size %%wx%%h or -extent %%wx%%h, assuming I am interpreting your commands correctly. If you have variables for w and h already, then you can specify them in -size or -extent, but you cannot compute them while doing -size or -extent.

Also perhaps you should first split your command into several commands with specific output files and get that to work, before you try to put them all together into one complex command line. That way, if you need to, you can compute image sizes as variables between steps for use in the next step.
wc2005
Posts: 6
Joined: 2011-04-26T01:44:31-07:00
Authentication code: 8675308

Re: How to get image size?

Post by wc2005 »

Yeah, I had some problems with .bat file, I searched around and it now works. Thanks a lot for your help! :D
Post Reply