使用 Python 从图像中提取 gps 坐标
Extracting gps coordinates from image using Python
from PIL import Image
from PIL.ExifTags import TAGS
# path to the image or video
imagename = "image.jpg"
# read the image data using PIL
image = Image.open(imagename)
# extract EXIF data
exifdata = image.getexif()
# iterating over all EXIF data fields
for tag_id in exifdata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
print(f"{tag:25}: {data}")
Exif版本:0220
组件配置:
快门速度值:(1345, 100)
原始日期时间:2020:08:27 09:43:15
日期时间数字化:2020:08:27 09:43:15
光圈值:(185、100)
亮度值:(930, 100)
曝光偏差值:(0、10)
最大光圈值:(185, 100)
测光模式:2
闪光灯:0
焦距:(358、100)
用户评论:
色彩空间:1
ExifImageWidth:4128
场景捕捉类型:0
亚秒时间:0424
SubsecTimeOriginal:0424
亚秒时间数字化:0424
ExifImageHeight:1908
图片长度:1908
品牌:三星
型号 : SM-M305F
方向 : 6
YCbCr定位:1
曝光时间:(1, 2786)
ExifInteroperabilityOffset:944
X分辨率:(72, 1)
F 数 : (190, 100)
场景类型:
YResolution : (72, 1)
ImageUniqueID : E13LLLI00PM E13LLMK03PA
曝光程序:2
自定义渲染:0
ISO速度等级:40
分辨率单位:2
曝光模式:0
FlashPix版本:0100
图片宽度:4128
白平衡:0
软件:M305FDDU5CTF2
日期时间:2020:08:27 09:43:15
数字缩放比例:(0, 0)
FocalLengthIn35mmFilm : 27
对比度:0
饱和度:0
清晰度:0
Exif偏移量:226
创客笔记:0100 Z@P
使用模块piexif (pip install piexif
)您可以获取exif中的GPS信息,如下所示。
from pprint import pprint
from PIL import Image
import piexif
codec = 'ISO-8859-1' # or latin-1
def exif_to_tag(exif_dict):
exif_tag_dict = {}
thumbnail = exif_dict.pop('thumbnail')
exif_tag_dict['thumbnail'] = thumbnail.decode(codec)
for ifd in exif_dict:
exif_tag_dict[ifd] = {}
for tag in exif_dict[ifd]:
try:
element = exif_dict[ifd][tag].decode(codec)
except AttributeError:
element = exif_dict[ifd][tag]
exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element
return exif_tag_dict
def main():
filename = 'IMG_2685.jpg' # obviously one of your own pictures
im = Image.open(filename)
exif_dict = piexif.load(im.info.get('exif'))
exif_dict = exif_to_tag(exif_dict)
pprint(exif_dict['GPS'])
if __name__ == '__main__':
main()
结果
{'GPSAltitude': (94549, 14993),
'GPSAltitudeRef': 0,
'GPSDateStamp': '2020:09:04',
'GPSDestBearing': (1061399, 5644),
'GPSDestBearingRef': 'T',
'GPSHPositioningError': (5, 1),
'GPSImgDirection': (1061399, 5644),
'GPSImgDirectionRef': 'T',
'GPSLatitude': ((12, 1), (34, 1), (1816, 100)),
'GPSLatitudeRef': 'N',
'GPSLongitude': ((99, 1), (57, 1), (4108, 100)),
'GPSLongitudeRef': 'E',
'GPSSpeed': (0, 1),
'GPSSpeedRef': 'K',
'GPSTimeStamp': ((13, 1), (2, 1), (4599, 100)),
'GPSVersionID': (2, 2, 0, 0)}
此处 exif_to_tag
将 exif 代码转换为更具可读性的标签。
from PIL import Image
from PIL.ExifTags import TAGS
# path to the image or video
imagename = "image.jpg"
# read the image data using PIL
image = Image.open(imagename)
# extract EXIF data
exifdata = image.getexif()
# iterating over all EXIF data fields
for tag_id in exifdata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
print(f"{tag:25}: {data}")
Exif版本:0220 组件配置: 快门速度值:(1345, 100) 原始日期时间:2020:08:27 09:43:15 日期时间数字化:2020:08:27 09:43:15 光圈值:(185、100) 亮度值:(930, 100) 曝光偏差值:(0、10) 最大光圈值:(185, 100) 测光模式:2 闪光灯:0 焦距:(358、100) 用户评论: 色彩空间:1 ExifImageWidth:4128 场景捕捉类型:0 亚秒时间:0424 SubsecTimeOriginal:0424 亚秒时间数字化:0424 ExifImageHeight:1908 图片长度:1908 品牌:三星 型号 : SM-M305F 方向 : 6 YCbCr定位:1 曝光时间:(1, 2786) ExifInteroperabilityOffset:944 X分辨率:(72, 1) F 数 : (190, 100) 场景类型: YResolution : (72, 1) ImageUniqueID : E13LLLI00PM E13LLMK03PA
曝光程序:2 自定义渲染:0 ISO速度等级:40 分辨率单位:2 曝光模式:0 FlashPix版本:0100 图片宽度:4128 白平衡:0 软件:M305FDDU5CTF2 日期时间:2020:08:27 09:43:15 数字缩放比例:(0, 0) FocalLengthIn35mmFilm : 27 对比度:0 饱和度:0 清晰度:0 Exif偏移量:226 创客笔记:0100 Z@P
使用模块piexif (pip install piexif
)您可以获取exif中的GPS信息,如下所示。
from pprint import pprint
from PIL import Image
import piexif
codec = 'ISO-8859-1' # or latin-1
def exif_to_tag(exif_dict):
exif_tag_dict = {}
thumbnail = exif_dict.pop('thumbnail')
exif_tag_dict['thumbnail'] = thumbnail.decode(codec)
for ifd in exif_dict:
exif_tag_dict[ifd] = {}
for tag in exif_dict[ifd]:
try:
element = exif_dict[ifd][tag].decode(codec)
except AttributeError:
element = exif_dict[ifd][tag]
exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element
return exif_tag_dict
def main():
filename = 'IMG_2685.jpg' # obviously one of your own pictures
im = Image.open(filename)
exif_dict = piexif.load(im.info.get('exif'))
exif_dict = exif_to_tag(exif_dict)
pprint(exif_dict['GPS'])
if __name__ == '__main__':
main()
结果
{'GPSAltitude': (94549, 14993),
'GPSAltitudeRef': 0,
'GPSDateStamp': '2020:09:04',
'GPSDestBearing': (1061399, 5644),
'GPSDestBearingRef': 'T',
'GPSHPositioningError': (5, 1),
'GPSImgDirection': (1061399, 5644),
'GPSImgDirectionRef': 'T',
'GPSLatitude': ((12, 1), (34, 1), (1816, 100)),
'GPSLatitudeRef': 'N',
'GPSLongitude': ((99, 1), (57, 1), (4108, 100)),
'GPSLongitudeRef': 'E',
'GPSSpeed': (0, 1),
'GPSSpeedRef': 'K',
'GPSTimeStamp': ((13, 1), (2, 1), (4599, 100)),
'GPSVersionID': (2, 2, 0, 0)}
此处 exif_to_tag
将 exif 代码转换为更具可读性的标签。