Search code examples
graphicsmagick

gm identify syntax get image width and height


I am trying to get the height and width of a jpeg image in the commandline.

I typed gm identify img300.jpg and get a long line that outputs JPEG 3264x2448+0+0 DirectClass 8-bit 1.8Mi 0.000u 0m:0.000002s

I looked in the manual for the gm command and it says I can get just the image dimensions with the -density option.

http://www.graphicsmagick.org/identify.html#ident-opti

So I tried gm identify -density img300.jpg [Option requires an arguement]

gm identify img300.jpg -density [no such file or directory]

debian 9, latest graphicsmagick package is the environment.


Solution

  • You can get the width like this:

    gm identify -format %w image.png
    256
    

    And the height like this:

    gm identify -format %h image.png
    80
    

    If you want height in a variable:

    h=$(gm identify -format %h image.png)
    

    If you want both in variables in one go:

    read w h < <(gm identify -format "%w %h" image.png )
    echo $w, $h