通過第三方庫exifread讀取照片信息。
exifread官網:https://pypi.org/project/ExifRead/
一、安裝exifread
pip install exifread
二、讀取照片信息,以及根據經緯度通過百度地圖API獲取位置
?
import
exifread
import
json
import
urllib.request
#
Open image file for reading (binary mode)
f = open(
'
001.jpg
'
,
'
rb
'
)
#
Return Exif tags
tags =
exifread.process_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
=
'
http://api.map.baidu.com/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))
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

