相机拍摄照片会在文件中记录拍摄的重要参数,包括机身信息、光圈、焦距、感光度、时间、拍摄地点等等。本文介绍获取此类信息的方法。
安装python包
代码语言:javascript复制安装exifread:
pip install exifread
上代码
代码语言:javascript复制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}")
代码语言:javascript复制示例输出
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下载。