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:

File name    : foo.jpg
File size    : 463023 bytes
File date    : 2001:08:12 21:02:04
Camera make  : Canon
Camera model : Canon PowerShot S100
Date/Time    : 2001:08:05 15:39:33
Resolution   : 1600 x 1200
Flash used   : No
Focal length :  5.4mm  (35mm equivalent: 36mm)
CCD Width    : 5.23mm
Exposure time: 0.100 s  (1/10)
Aperture     : f/2.8
Focus Dist.  : 1.18m
Metering Mode: center weight
Jpeg process : Baseline
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 stitcher.


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.

If you have trouble finding the sensor size, please contact the TAs.


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.