# -*- coding: cp936 -*-
#
#WriteTo:yilong594@hotmail.com
#http://download.youkuaiyun.com/detail/testemule/4622088中的源码找不着了,重新使用PIL写的一份
from PIL import Image
from PIL.ExifTags import TAGS
import glob,os
if os.path.exists('NoGPSinfo.txt'):
os.remove('NoGPSinfo.txt')
if os.path.exists('result.txt'):
os.remove('result.txt')
def get_exif(fn):
im=Image.open(fn)
info = im._getexif()
ret = {}
try:
for tag, value in info.items():
ret[TAGS.get(tag, tag)] = value
except:
ret={}
if ret!={}:
gpsinfo=ret.get('GPSInfo')
if gpsinfo!=None :
LAT=gpsinfo.get(2)
LON=gpsinfo.get(4)
if LAT [0][0]!=0 and LON[0][0]!=0:
Lat0=float(LAT[0][0])/LAT[0][1]+float(LAT[1][0])/LAT[1][1]/60+float(LAT[2][0])/LAT[2][1]/3600
Lon0=float(LON[0][0])/LAT[0][1]+float(LON[1][0])/LAT[1][1]/60+float(LON[2][0])/LAT[2][1]/3600
Height=float(gpsinfo.get(6)[0])/gpsinfo.get(6)[1]
Lat=round(Lat0,6)
Lon=round(Lon0,6)
open('result.txt','a').write(fn+' '+str(Height)+' '+str(Lat)+' '+str(Lon)+'\n')
else:
open('NoGPSinfo.txt','a').write(fn+' has no GPS info\n')
else:
open('NoGPSinfo.txt','a').write('Maybe '+fn +' is not a digital photo~\n')
flist=glob.glob('*.jpg')
for x in flist:
get_exif(x)
if os.path.exists('result.txt'):
f=open('result.txt')
f2=open('track_point.kml','w')
print >>f2,"""<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>track_point</name>"""
for x in f.readlines():
x=x.strip().split(' ')
print >>f2,"""<Placemark>
<description><![CDATA[<html>
<body><img src=".\%s" width="428" height="284" />
</body></html>]]></description>
<name>%s</name>
<Point>
<Icon>
<href>%s</href>
</Icon>
<coordinates>%s,%s,%s</coordinates>
</Point>
</Placemark>"""%(x[0],x[0],x[0],x[3],x[2],x[1])
f.close()
print >>f2,'</Document></kml>'
f2.close()
输出照片中的GPS信息到文本文件,更新之前写的较复杂的程序
最新推荐文章于 2023-11-22 20:00:00 发布
本文介绍了如何利用Python的PIL库解析图片的EXIF信息,提取GPS坐标,并将这些坐标数据转换为KML格式,用于生成轨迹文件。通过遍历图片文件夹,对每张图片进行解析,获取高度、纬度和经度等关键信息,最终将这些信息以KML文件的形式呈现,便于地图应用中展示轨迹。
1798

被折叠的 条评论
为什么被折叠?



