Get Image Dimensions from Command Line

By  on  

The command line is a gold mine if you come from the perspective of a UI lover.  Getting information from the shell instead of opening an app, finding a file or directory, etc...what a novel concept.  Opening different image files opens up different apps on my Mac and, as the kids say, "ffs" -- I just want to know the image dimensions.

Using ImageMagick you can find the dimensions of an image from command line:

# Get the size of a JPG
convert photo.jpg -print "Size: %wx%h\n" /dev/null
# Size: 600x872

# Get the size of a PSD
convert website-design.psd -print "Size: %wx%h\n" /dev/null
# Size: 990x1200

You can get the image dimensions of any image type from PNG to JPG to GIF to even PSDs.  And the resulting text is as plain as it could be.  Dimensions...here you are.

Recent Features

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Drag & Drop Elements to the Trash with MooTools 1.2

    Everyone loves dragging garbage files from their desktop into their trash can. There's a certain amount of irony in doing something on your computer that you also do in real life. It's also a quick way to get rid of things. That's...

  • By
    Spyjax:  Ajax For Evil Using Dojo

    The idea of Spyjax is nothing new. In pasts posts I've covered how you can spy on your user's history with both MooTools and jQuery. Today we'll cover how to check user history using the Dojo Toolkit. The HTML For the sake of this...

Discussion

  1. I like to use ImageMagick’s identify command. Easy to remember and gives the size along with some other helpful info:

    identify photo.jpg
    
  2. ennkay

    but that means having ImageMagick installed.

    on a mac you have the native sips command that returns info and modifies image files.
    ex. to get all sorts of info on an image just do:

    sips -g all  /Users/your_account/image_path.mime

    more info on sips available at:
    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sips.1.html

    • Wow, thanks for the heads up! I didn’t know about sips!

  3. Jose Miguel Pérez

    Wow! I always wonder why the command file is so unknown on the Mac?

    $ file test.psd
    test.psd: Adobe Photoshop Image, 918 x 445, RGB, 3x 8-bit channels
    

    No need to install anything! Use man file for more information. Works for every kind of files, not just images:

    $ find . -print0 | xargs -0 file
    .:                                     directory
    ./.DS_Store:                           Apple Desktop Services Store
    ./Designs:                             directory
    ./Designs/.DS_Store:                   Apple Desktop Services Store
    ./Designs/Web Structure.txt:           UTF-8 Unicode text
    ./Designs/WebContents.docx:            Microsoft Word 2007+
    ./Designs/WebContents.rar:             RAR archive data, v1d, os: Win32
    ./Designs/OCMWeb02:                    directory
    ./Designs/OCMWeb02/.DS_Store:          Apple Desktop Services Store
    ./Designs/OCMWeb02/OcmWeb07Copy.png:   PNG image data, 973 x 984, 8-bit/color RGB, non-interlaced
    ./Designs/OCMWeb02/OcmWeb10Backup.png: PNG image data, 975 x 877, 8-bit/color RGB, non-interlaced
    ./Designs/Logos - Testing 2.ai:        PDF document, version 1.5
    ./Designs/Testing Cards.ai:            PDF document, version 1.5
    [...]
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!