Python - 获取图像Exif详细信息

本文最后更新于:2022年8月5日 晚上

相机拍摄照片会在文件中记录拍摄的重要参数,包括机身信息、光圈、焦距、感光度、时间、拍摄地点等等。本文介绍获取此类信息的方法。

安装python包

安装exifread:

1
pip install exifread

上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import exifread
import requests


class PhotoExifInfo():

def __init__(self,photo_path):
self.photo_path = photo_path
self.baidu_map_ak = "your baidu map api key"
self.image_info_dict={}
self.tags ={}
self.interested_keys = [
'EXIF ExposureMode',\
'EXIF ExposureTime',\
'EXIF Flash',\
'EXIF ISOSpeedRatings',\
'Image Model',\
'EXIF ExifImageWidth',\
'EXIF ExifImageLength',\
'Image DateTime',\
'EXIF DateTimeOriginal',\
'Image Make',\

# lens
# jiaoju
]


def get_tags(self):
"""
获取照片信息
"""
image_content = open(self.photo_path, 'rb')
tags = exifread.process_file(image_content)
self.tags = tags

for item in self.interested_keys:
try:
info = tags[item]
self.image_info_dict[item] = info
except:
print(f'{self.photo_path} has no attribute of {item}')
continue

# 遍历获取照片所有信息
#for j, k in tags.items():
#print(f"{j} : {k}")
#print('拍摄时间:', tags['EXIF DateTimeOriginal'])
#print('照相机制造商:', tags['Image Make'])
#print('照相机型号:', tags['Image Model'])
#print('照片尺寸:', tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength'])


image_content.close()


def get_lng_lat(self):
"""经纬度转换"""
tags = self.tags
try:
# 纬度
LatRef = tags["GPS GPSLatitudeRef"].printable
Lat = tags["GPS GPSLatitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
Lat = float(Lat[0]) + float(Lat[1]) / 60 + float(Lat[2]) / 3600
if LatRef != "N":
Lat = Lat * (-1)
# 经度
LonRef = tags["GPS GPSLongitudeRef"].printable
Lon = tags["GPS GPSLongitude"].printable[1:-1].replace(" ", "").replace("/", ",").split(",")
Lon = float(Lon[0]) + float(Lon[1]) / 60 + float(Lon[2]) / 3600
if LonRef != "E":
Lon = Lon * (-1)
return Lat,Lon
except:
print('Unable to get')



def get_city_info(self):
result = self.get_lng_lat()
if result:
Lat, Lon = result
url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=" + str(Lat) + ',' + str(Lon)
#url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak="+self.baidu_map_ak+"&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194"
response = requests.get(url).json()
status = response['status']
if status == 0:
address = response['result']['formatted_address']
if address != "":
self.image_info_dict['Position'] = address
else:
print('baidu_map error')



def get_image_info(self):
self.get_tags()
self.get_city_info()
return self.image_info_dict



if __name__ == '__main__':
result = PhotoExifInfo("test.jpeg").get_image_info()

for j, k in result.items():
print(f"{j} : {k}")

示例输出

1
2
3
4
5
6
7
8
9
10
11
EXIF ExposureMode : Auto Exposure
EXIF ExposureTime : 10/24723
EXIF Flash : Flash did not fire, auto mode
EXIF ISOSpeedRatings : 50
Image Model : vivo Z1
EXIF ExifImageWidth : 4144
EXIF ExifImageLength : 1968
Image DateTime : 2019:11:03 11:34:24
EXIF DateTimeOriginal : 2019:11:03 11:34:24
Image Make : vivo
Position : 上海市黄浦区中山南路187

代码中的self.baidu_map_ak需要去百度地图申请,具体方法移步: 申请百度地图API Key进行百度地图开发,获取经纬度对应地点

获取源码

文中测试环境与所有源码可在Github下载。


Python - 获取图像Exif详细信息
https://www.zywvvd.com/notes/coding/python/get-image-exif/get-image-exif/
作者
Yiwei Zhang
发布于
2020年4月1日
许可协议