Current version: 1.9
This is the homepage of ImageInfo, a free Java class to retrieve properties from image files.
The following file formats are currently supported: JPEG, PNG, GIF, BMP, PCX, IFF, RAS, PBM, PGM, PPM and PSD
(read why TIFF is not included).
ImageInfo can recognize these formats, and in addition determine image width,
height and color depth (bits per pixel).
Also check out ffident, my file format (group) and MIME type identification library. It extracts less information than ImageInfo (it only recognizes the format, no metadata), but it supports more formats and new formats can be added by editing a text file. Similar to file(1) / magic(5).
ImageInfo from within a Java application or applet
The image file can be any InputStream object or an instance of a
class implementing DataInput (like RandomAccessFile).
Here is some sample code on how to use the class:
ImageInfo ii = new ImageInfo();
// in can be InputStream or RandomAccessFile (or DataInput)
ii.setInput(in);
/* if you want to know how many images there are in a file,
uncomment the following line; will slow down ImageInfo
with animated GIFs */
//ii.setDetermineImageNumber(true);
// check does the actual work, you won't get results before
// you have called it
if (!ii.check())
{
System.err.println("Not a supported image file format.");
}
else
{
System.out.println(
ii.getFormatName() + ", " +
ii.getMimeType() + ", " +
ii.getWidth() + " x " + ii.getHeight() + " pixels, " +
ii.getBitsPerPixel() + " bits per pixel, " +
ii.getNumberOfImages() + " image(s).");
// there are other properties, check out the API documentation
}
ImageInfo as a command line program
ImageInfo also has a main method that makes it a command line tool.
Assuming that ImageInfo.class is in your classpath, giving the class to
java with some file names as arguments will be sufficient.
Here is an example call including output:
$ java ImageInfo test.jpg
test.jpg
File format: JPEG
MIME type: image/jpeg
Width (pixels): 1556
Height (pixels): 2048
Bits per pixel: 24
Progressive: false
Number of images: 1
Physical width (dpi): 300
Physical height (dpi): 300
Physical width (inches): 5.1866665
Physical height (inches): 6.826667
ImageInfo?
ImageInfo does not depend on the AWT.
It only needs some basic I/O classes from the standard package java.io,
only classes that have been there since Java 1.0.
It checks certain signatures ("magic bytes") and retrieves
the image properties, which rarely requires reading more than a hundred bytes.
Thus, it is relatively fast and well-suited to be used in a server environment (e.g. from within a
servlet that wants to know about the resolution of an image without loading it).
Getting an image's resolution by loading it via java.awt.Toolkit is less recommendable:
Toolkit understands fewer file formats than ImageInfo.Most of the supported formats have relatively long signatures so that file can be reliably identified. However, there can be cases where files in a format are (1) recognized as being in another format or (2) are not recognized at all. Both error types are unlikely, though. Please tell me if you encounter problems of this kind.
Besides, ImageInfo only checks some data at the beginning of the file. It does not check if the file is truncated or otherwise corrupted. See my tool IdentifyTruncatedJpeg to determine truncated JPEGs.
ImageInfo requires Java 1.1 or higher.
In fact, it may work with Java 1.0.x as well, but I cannot test that.
ImageInfo is put into the Public Domain.
However, there are some things you can do to.
An extensive list of changes from version to version is kept in the class comment of the source code file ImageInfo.java. It is no longer replicated here (too tedious to maintain).
This class has a Freshmeat project entry. If you have a login (it's free), you can use the Subscribe to new releases link on that project page to be notified of new versions of ImageInfo.
Download source code, bytecode and an HTML API documentation file created by javadoc as a single ZIP archive: imageinfo.zip (28 KB).
I will not add new features or formats to ImageInfo, see my thoughts on that topic.
TIFF is probably the only popular file format missing from the list of formats supported by ImageInfo. Here's why.
One could try to read the complete InputStream into a buffer, but that only works with smaller TIFFs.
Another approach would be to give up if the offset of values to be
retrieved from outside the IFD lie before the IFD that one has read
already (which is not uncommon, as far as I know).
But I want to support a format either fully or not at all.
Users would be irritated by the fact that a.tif is recognized
but b.tif is not ("b.tif does not seem to be a TIFF file"),
although both are perfectly valid TIFF files.
A lot of very interesting metadata gets stored in JPEG files using either
EXIF headers (mostly information related
to a digital camera) or special Photoshop markers.
ImageInfo does not extract either of those.
They are too specialized and, similar to TIFF, too complicated
for a single class.
However, there is Java code to extract that information, see
my JPEG format page for links (under Metadata libraries).
I've written an answer in the comments section of ImageInfo's Freshmeat already, but I'll put a short statement here as well. ImageInfo has started out in 2001 as a quick and lightweight solution for retrieving width and height from JPEG, GIF and PNG files. The weight is still relatively light, but a lot of features have been added. I wanted to keep the simplicity of "one class only". However, at 1300+ lines ImageInfo has become a bad example in terms of software design. Methods are too long and complex, each format should have a class of its own (plugin system), input and output should be independent from the rest, and so on.
I'm aware of all this, but a real rewrite would mean creating some sort of metadata extraction library. Which I find interesting, and it would certainly find its users. However, I'm not going to write such a thing anytime soon. In fact, I have written something like this on a smaller level but can't give away the code. With ImageInfo, I'll only add bug fixes.
Please read my article on physical image resolution first. It explains the topic in detail.
Note that some formats don't support certain properties.
In other formats, certain properties are only optional.
I sometimes get mail that ImageInfo does not determine correctly that
a particular JPEG file has 72 x 72 dpi (application X says that the file has that dpi resolution).
However, when I get that JPEG file it turns out that there is no dpi information in it,
software X just displays some default values when it can't find anything in the file.
Not ImageInfo's fault.
Please read my article on physical image resolution first. It explains the topic in detail.
Note that some formats don't support certain properties. In other formats, certain properties are only optional. PNG files can optionally store the physical resolution in a "phys" chunk. ImageInfo does not read this chunk, so you will never get physical resolution from PNG files with ImageInfo. In order to retrieve physical resolution one must scan the chunks in a PNG file, wait for a phys chunk to come along and then read and interpret its content. I'm not planning to add that functionality. If you want to do that yourself, you may get some inspiration from the PNGCodec class of my JIU package.
The following project links are only listed because they may be helpful to site visitors. I do not maintain those projects and know nothing about them beyond what I wrote below.
There is a straight port of this Java project to C++ at http://www.geocities.com/imageinfo_cpp, with additional functionality.
A more recent port to C# is also available.
For similar code in Perl, see ImageSize.
For a Pascal implementation (JPEG only), see jpeginfo (currently not available).
Visual Basic code can be found at http://www.freevbcode.com/ShowCode.Asp?ID=112.
PHP code is at http://www.php.net/manual/en/function.getimagesize.php.