Wim ten Brink
Wim ten Brink

Reputation: 26682

How to get dimensions of image without loading the image

I think I'm asking for the impossible, but will ask anyways...
I have a folder (with several subfolders) which contains a lot of images in JPG and PNG formats. And I need a tool that creates a list of those files including their dimensions. (Width and height.) To do so, I can just load the image into a bitmap and read the Width/Height properties. This would be okay if the images themselves weren't so huge...
The standard image size is about 8 megapixels but about 10% is between 40 and 100 megapixels. (Yes, that's 10,000x10,000 pixels.) This means that I'm dealing with PNG files of 50 to 100 MB each. With a total of 7,500 images this means that my tool is quite busy reading a lot of file data.
I need a faster way to read the dimensions of these file types. Not because of the memory usage, since my system has 32 GB RAM, but because I don't want to wait 45 minutes for the index to be done... (Well, slight exaggeration, but it's not fast.)

Upvotes: 4

Views: 2650

Answers (2)

Adrian Salazar
Adrian Salazar

Reputation: 5319

You have a complete article and working code in the following link (CodeProject).

http://www.codeproject.com/Articles/35978/Reading-Image-Headers-to-Get-Width-and-Height

He is reading the headers information to get the dimensions of the image. Performance looks good.

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207818

Have a look at jhead here. or exiv2, see here.

It is pretty quick and you could use something like "system()" to run it and parse the output.

Sample output:

File name    : b.jpg
File size    : 643664 bytes
File date    : 2014:02:19 13:07:29
Camera make  : NIKON CORPORATION
Camera model : NIKON D2Xs
Date/Time    : 2007:08:01 11:02:28
Resolution   : 1200 x 797
Flash used   : No
Focal length : 28.0mm  (35mm equivalent: 42mm)
Exposure time: 0.0016 s  (1/640)
Aperture     : f/7.1
ISO equiv.   : 250
Whitebalance : Auto
Metering Mode: center weight
Exposure     : shutter priority (semi-auto)
GPS Latitude : N 52d 13.3690m  0s
GPS Longitude: W  0d 52.7070m  0s
GPS Altitude :  456.00m

Exiv2 handles PNG files as well, and is C-callable as a library so maybe more elegant.

Upvotes: 1

Related Questions