来源:https://www.cnblogs.com/baby123/p/12213794.html
昨天听人说,用手机拍照会带着GPS信息,原来没注意过这个,因此查看下并使用代码获取照片里的GPS信息
说明:
一般手机拍照时默认会打开地理位置开关
经过压缩后,通常会将GPS信息压缩掉
EXIF
可交换图像文件常被简称为EXIF(Exchangeable image file format),是专门为数码相机的照片设定的,可以记录数码照片的属性信息和拍摄数据
注:
EXIF信息不支持png,webp等图片格式
python通过exifread模块获得图片exif信息
ExifRead
Python library to extract EXIF data from tiff and jpeg files.
安装
pip install exifread
读取GPS
import exifread
import re
def read():
GPS = {}
date = ''
f = open("E:\\python\\IMG_20200119_145630.jpg",'rb')
contents = exifread.process_file(f)
for key in contents:
if key == "GPS GPSLongitude":
print("经度 =", contents[key],contents['GPS GPSLatitudeRef'])
elif key =="GPS GPSLatitude":
print("纬度 =",contents[key],contents['GPS GPSLongitudeRef'])
read()
运行
读取更多信息
import exifread
import re
def read():
GPS = {}
date = ''
f = open("E:\\python\\IMG_20200119_145630.jpg",'rb')
contents = exifread.process_file(f)
for key in contents:
if key == "GPS GPSLongitude":
print("经度: ", contents[key],contents['GPS GPSLatitudeRef'])
print("纬度: ",contents['GPS GPSLatitude'],contents['GPS GPSLongitudeRef'])
print("高度基准: ",contents['GPS GPSAltitudeRef'])
print("海拔高度: ",contents['GPS GPSAltitude'])
if re.match('Image Make', key):
print('品牌信息: ' , contents[key])
if re.match('Image Model', key):
print('具体型号: ' , contents[key])
if re.match('Image DateTime', key):
print('拍摄时间: ' , contents[key])
if re.match('EXIF ExifImageWidth', key):
print('照片尺寸: ' , contents[key],'*',contents['EXIF ExifImageLength'])
if re.match('Image ImageDescription',key):
print('图像描述: ' , contents[key])
read()