###### 从JPG文件的EXIF信息估计焦距

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:

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

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.

## 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.

#### 利用照片携带的EXIF来估计焦距等信息

2014-12-19 14:18:08

#### 读取JPG图片的Exif属性（一） - Exif信息简介

2016-12-13 08:47:27

#### linux下jpeg图片Exif标识

2016-09-20 11:37:34

#### 通过Exiv2读取照片的Exif信息获取GPS，焦距等信息

2017-10-01 16:22:35

#### Exif格式分析

2013-02-20 10:16:29

#### 等效于35mm相机焦距的计算方法

2015-06-01 13:20:49

#### Exif的使用

2015-08-03 15:05:01

#### 图片文件Exif信息详细说明

2016-03-24 17:34:01

#### Exif文件格式描述 Ver1.4

2016-09-05 23:26:27

#### 焦距

2016-10-15 22:17:15