读取照片信息,以及根据经纬度通过百度地图API获取位置
import exifread
import json
import urllib.request
Open image file for reading (binary mode)
f=open(‘001.jpg’, ‘rb’)
Return Exif tags
tags=exifreadcess_file(f)
‘’’
#打印所有照片信息
for tag in tags.keys():
print(“Key: {}, value {}”.format(tag, tags[tag]))
‘’’
#打印照片其中一些信息
print(‘拍摄时间:’, tags[‘EXIF DateTimeOriginal’])
print(‘照相机制造商:’, tags[‘Image Make’])
print(‘照相机型号:’, tags[‘Image Model’])
print(‘照片尺寸:’, tags[‘EXIF ExifImageWidth’], tags[‘EXIF ExifImageLength’])
#获取经度或纬度
def getLatOrLng(refKey, tudeKey):
if refKey not in tags:
return None
ref=tags[refKey].printable
LatOrLng=tags[tudeKey].printable[1:-1].replace(" “,”").replace("/",",").split(",")
LatOrLng=float(LatOrLng[0])+float(LatOrLng[1])/60+float(LatOrLng[2])/float(LatOrLng[3])/3600
if refKey==‘GPS GPSLatitudeRef’ and tags[refKey].printable !=“N”:
LatOrLng=LatOrLng*(-1)
if refKey==‘GPS GPSLongitudeRef’ and tags[refKey].printable !=“E”:
LatOrLng=LatOrLng*(-1)
return LatOrLng
#调用百度地图API通过经纬度获取位置
def getlocation(lat,lng):
url=‘
api.map.baidu/geocoder/v2/?location=’ + lat + ‘,’ + lng + ‘&output=json&pois=1&ak=申请的百度地图KEY’
req=urllib.request.urlopen(url)
res=req.read().decode(“utf-8”)
str=json.loads(res)
#print(str)
jsonResult=str.get(‘result’)
formatted_address=jsonResult.get(‘formatted_address’)
return formatted_address
lat=getLatOrLng(‘GPS GPSLatitudeRef’,‘GPS GPSLatitude’) #纬度
lng=getLatOrLng(‘GPS GPSLongitudeRef’,‘GPS GPSLongitude’) #经度
print(‘纬度:{} 经度:{}’.format(lat, lng))
location=getlocation(str(lat), str(lng))
print(‘位置:{}’.format(location))
读取的图片信息大致包含以下内容:
{'Image ImageWidth': (0x0100) Short=4032 @ 18,
'Image Model': (0x0110) ASCII=****@ 158,
'Image ImageLength': (0x0101) Short=3024 @ 42,
'Image Orientation': (0x0112) Short=Horizontal (normal) @ 54,
'Image DateTime': (0x0132) ASCII=2021:12:07 03:10:34 @ 166,
'Image YCbCrPositioning': (0x0213) Short=Centered @ 78,
'Image ExifOffset': (0x8769) Long=209 @ 90,
'Image ResolutionUnit': (0x0128) Short=Pixels/Inch @ 102,
'Image GPSInfo': (0x8825) Long=792 @ 114,
'Image XResolution': (0x011A) Ratio=72 @ 186,
'Image YResolution': (0x011B) Ratio=72 @ 194,
'Image Make': (0x010F) ASCII=Xiaomi @ 202,
'Thumbnail JPEGInterchangeFormat': (0x0201) Long=928 @ 808,
'Thumbnail Orientation': (0x0112) Short=Horizontal (normal) @ 820,
'Thumbnail JPEGInterchangeFormatLength': (0x0202) Long=8039 @ 832,
'Thumbnail Compression': (0x0103) Short=JPEG (old-style) @ 844,
'Thumbnail ResolutionUnit': (0x0128) Short=Pixels/Inch @ 856,
'Thumbnail XResolution': (0x011A) Ratio=72 @ 912,
'Thumbnail YResolution': (0x011B) Ratio=72 @ 920,
'Thumbnail ExifImageLength': (0xA003) Long=240 @ 892,
'Thumbnail ExifImageWidth': (0xA002) Long=320 @ 904,
'EXIF ISOSpeedRatings': (0x8827) Long=372 @ 219,
'EXIF ExposureProgram': (0x8822) Short=Program Normal @ 231,
'EXIF FNumber': (0x829D) Ratio=19/10 @ 599, 'EXIF ExposureTime': (0x829A) Ratio=1/50 @ 607,
'EXIF Tag 0x9999': (0x9999) ASCII={"sensor_type":"rear","mirror":false} @ 615,
'EXIF SensingMethod': (0xA217) Short=Not defined @ 279,
'EXIF SubSecTimeDigitized': (0x9292) ASCII=183622 @ 653,
'EXIF SubSecTimeOriginal': (0x9291) ASCII=183622 @ 660,
'EXIF SubSecTime': (0x9290) ASCII=183622 @ 667,
'EXIF FocalLength': (0x920A) Ratio=197/50 @ 674,
'EXIF Flash': (0x9209) Short=Flash did not fire, compulsory flash mode @ 339,
'EXIF LightSource': (0x9208) Short=Unknown @ 351,
'EXIF MeteringMode': (0x9207) Short=CenterWeightedAverage @ 363,
'EXIF SceneCaptureType': (0xA406) Short=Standard @ 375,
'Interoperability InteroperabilityIndex': (0x0001) ASCII=R98 @ 772,
'Interoperability InteroperabilityVersion': (0x0002) Undefined=[48, 49, 48, 48] @ 784,
'EXIF InteroperabilityOffset': (0xA005) Long=762 @ 387,
'EXIF FocalLengthIn35mmFilm': (0xA405) Short=20 @ 399,
'EXIF MaxApertureValue': (0x9205) Ratio=37/20 @ 682,
'EXIF DateTimeDigitized': (0x9004) ASCII=2021:12:07 03:10:34 @ 690,
'EXIF ExposureBiasValue': (0x9204) Signed Ratio=0 @ 710,
'EXIF ExifImageLength': (0xA003) Long=3024 @ 447,
'EXIF WhiteBalance': (0xA403) Short=Auto @ 459,
'EXIF DateTimeOriginal': (0x9003) ASCII=2021:12:07 03:10:34 @ 718,
'EXIF BrightnessValue': (0x9203) Signed Ratio=-79/50 @ 738,
'EXIF ExifImageWidth': (0xA002) Long=4032 @ 495,
'EXIF ExposureMode': (0xA402) Short=Auto Exposure @ 507,
'EXIF ApertureValue': (0x9202) Ratio=37/20 @ 746,
'EXIF ComponentsConfiguration': (0x9101) Undefined=YCbCr @ 531,
'EXIF ColorSpace': (0xA001) Short=sRGB @ 543,
'EXIF SceneType': (0xA301) Byte=Directly Photographed @ 555,
'EXIF ShutterSpeedValue': (0x9201) Signed Ratio=5643/1000 @ 754,
'EXIF ExifVersion': (0x9000) Undefined=0220 @ 579, 'EXIF FlashPixVersion': (0xA000) Undefined=0100 @ 591,
'JPEGThumbnail': b'\xff\xd8\xf