输出照片中的GPS信息到文本文件,更新之前写的较复杂的程序

本文介绍了如何利用Python的PIL库解析图片的EXIF信息,提取GPS坐标,并将这些坐标数据转换为KML格式,用于生成轨迹文件。通过遍历图片文件夹,对每张图片进行解析,获取高度、纬度和经度等关键信息,最终将这些信息以KML文件的形式呈现,便于地图应用中展示轨迹。
# -*- 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()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值