Estimating the focal length of a photo from EXIF tags

Noah Snavely, January 2008

When you take a photo with almost any digital camera, the camera embeds useful information about the photo into the JPEG file itself. This often includes information about exposure time, focus, aperture, whether the flash was activated, and focal length. The last of these, focal length, is especially useful when creating panoramas. This page describes how to extract the focal length from the EXIF tags of a digital photo and to convert it to pixel units, so that it can be used in the panorama stitcher.


Step One: Extracting the EXIF tags

First you'll need to extract the EXIF tags from the JPEG file (let's call it "foo.jpg"). To do so, we need an EXIF reader. There are many programs that will do this, but I recommend "jhead" because it is lightweight and simple to use. You can download jhead here:

Jhead webpage
direct link to Windows executable

Once you have jhead, you can run "jhead foo.jpg" from the command line, which will output something like this:

Sample jhead output:

The field you're looking for is focal length. In this case, the focal length is listed as "5.4mm". We'll need to convert this to pixels to use this in the panorama sticher.

Step Two: Finding the CCD width

Once we have the focal length in millimeters, a simple formula can be used to convert to focal length in pixels. First, however, we'll need to know the width of the image sensor of your camera. Sometimes this is listed in the EXIF tags (see the "CCD Width" field in the example above). In my experience, this number can be wrong, so it's better to verify it online. A good place to find it is dpreview; if our camera was the one that took the image above, the Canon PowerShot S100, we would Google "dpreview Canon PowerShot S100 specifications." The second link that comes up is "Canon PowerShot S100 digital camera specifications"; this is the one we want. On the resulting page, search for "sensor size". For the S100, the sensor size is given as 1/2.7"" (5.27 x 3.96 mm). This is a little different from the CCD width given in the EXIF tags, but probably not enough to make a difference. I would use the width on dpreview (5.27mm), as it is usually more reliable.

Sometimes the CCD width on dpreview is given as a ratio of inches (e.g., 1/2.7""). The conversion from these units to millimeters is not straightforward, but you can look up the mapping here.


Step Three: Converting from millimeters to pixels

Once we know the focal length (in mm) and the CCD width (in mm), we can easily convert to focal length in pixels. The one last piece of information we need is the width of the image in pixels. This is given by jhead in the "Resolution" field; in the example above, the width in pixels is 1600. Now, to convert to focal length in pixels, we use the formula:

focal length in pixels = (image width in pixels) * (focal length in mm) / (CCD width in mm)

For the example photo above taken with the Canon PowerShot S100, this would be:

focal length in pixels = 1600 pixels * 5.4mm / 5.27mm   = 1639.47 pixels

So the focal length we would use for the stitcher is 1639.47.