Retrieve Height and Width of images

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
Dubben
Posts: 2
Joined: 2011-02-09T10:37:48-07:00
Authentication code: 8675308

Retrieve Height and Width of images

Post by Dubben »

Hi there,

I have a folder of 70,000+ images, and I need to retrieve the filename, and height/width in pixels of each image, and export them to a text file. I am very new to ImageMagick and programming so any guidance is appreciated.

Cheers.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Retrieve Height and Width of images

Post by fmw42 »

What version of IM do you have and on what platform? If on Unix/Mac, you can use string formats. See http://www.imagemagick.org/script/escape.php

You would have to write a (bash) script to loop over your images, compute the dimensions and write to file. Inside the loop:

convert image -format "%f %w %h" info: >> textfile

The text file will then contain one line for that image with the filename, space, width, space, height. It will append the same information for each image in the loop.

Other unix command alternatives to a bash script for the loop exist also. see http://www.imagemagick.org/Usage/basics/#mogrify_not

For windows users see http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Retrieve Height and Width of images

Post by anthony »

You can do this in a single pass

Code: Select all

identify -ping-format "%f %w %h\n" *.jpg  > image_info_file.txt
the \n is a newline.

The -ping option asks IM only to read enough of each image to retrieve the arrtibutes (if posible) and not load the full image into memory. Of course if you want something like color counts or other meta-data a 'ping' read may not be enough.

You can even format it in a more complex way such as HTML image tags.
For example this is from
http://www.imagemagick.org/Usage/basics/#identify

Code: Select all

 identify -ping -format '<IMG SRC="%i" ALT="%f" WIDTH=%w HEIGHT=%h>\n' \
            images...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Dubben
Posts: 2
Joined: 2011-02-09T10:37:48-07:00
Authentication code: 8675308

Re: Retrieve Height and Width of images

Post by Dubben »

Thank you both very much.

I ended up using:

Code: Select all

identify -ping -format "%f %w %h\n" *.jpg  > image_info_file.txt
which worked perfectly for my needs.
Post Reply