当前位置:首页 » 编程语言 » python3264

python3264

发布时间: 2024-09-18 03:31:46

① 使用python实时将gps返回的经纬度转化为图片

主要就是做了两件事情:
1.生成一张有文本信息的JPG图片
2.写入EXIF信息

生成照片需要PIL和libjpeg

import Imageimport ImageDrawimport ImageFontdef create_pic(path, text=[], type='jpeg'):
img = Image.new("RGB", (2448, 3264), '#37b6ce')#颜色和大小
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('ziti.ttf', 120)#ttf是字体,120是字号
for h in range(0, len(text)):#多行文本
draw.text((256, 256 + 120 * h), text[h], font=font)
img.save(path, type)#保存
# img.show()

读写EXIF信息需要pyexiv2,获取google的经纬度需要geopy
顺便说下经纬度的表示:
一般exif里看到的都是这样的57°55'56.6",是度,分,秒这么展示的,google上获取来的是十进制的57.9323888888888
所以需要转换一下再写进去
公式:57°55'56.6" =57+55/60+56.6/3600=57.9323888888888

etemplate=pyexiv2.ImageMetadata('IMG_4408.JPG')template.read()#exif信息很多,所以找个真正手机拍摄的照片当模版googlev3=geopy.GoogleV3()place,gps=googlev3.geocode(location)#获取gps信息,location写地名,比如‘北京王府井’,偶尔会被墙,最好挂个代理defset_exif(path,date_time=None,gps=()):
"""
datetime=2014:10:0412:41:38
geo=(lat=39.12315,lng=115.12231)
"""
metadata=pyexiv2.ImageMetadata(path)
metadata.read()
forkintemplate.exif_keys:
metadata[k]=pyexiv2.ExifTag(k,template[k].value)
ifnotdate_time:
date_str=pyexiv2.utils.exif(date_time)
metadata['Exif.Photo.DateTimeOriginal']=date_str
metadata['Exif.Photo.DateTimeDigitized']=date_str
metadata['Exif.Image.DateTime']=date_str
iflen(geo)>0:
c_lat=decimal2coordinate(geo[0],['S','N'])
c_lng=decimal2coordinate(geo[1],['W','E'])
metadata["Exif.GPSInfo.GPSLatitude"]=coordinate2rational(c_lat[0],c_lat[1],c_lat[2])
metadata["Exif.GPSInfo.GPSLatitudeRef"]=c_lat[3]
metadata["Exif.GPSInfo.GPSLongitude"]=coordinate2rational(c_lng[0],c_lng[1],c_lng[2])
metadata["Exif.GPSInfo.GPSLongitudeRef"]=c_lng[3]
else:
metadata._delete_exif_tag("Exif.GPSInfo.GPSLatitude")
metadata._delete_exif_tag("Exif.GPSInfo.GPSLatitudeRef")
metadata._delete_exif_tag("Exif.GPSInfo.GPSLongitude")
metadata._delete_exif_tag("Exif.GPSInfo.GPSLongitudeRef")
metadata.write()defdecimal2coordinate(value,loc):
"""
loc=lat=>["S","N"],lng=>["W","E"]
retrunD,M,S,locate
"""
ifvalue<0:
loc_value=loc[0]
elifvalue>0:
loc_value=loc[1]
else:
loc_value=""
abs_value=abs(value)
deg=int(abs_value)
t1=(abs_value-deg)*60
min=int(t1)
sec=round((t1-min)*60,5)
return(deg,min,sec,loc_value)defcoordinate2rational(D,M,S):
return(fractions.Fraction(D,1),fractions.Fraction(int((M+S/60)*100),100),fractions.Fraction(0,1))
热点内容
数据库财经 发布:2024-09-19 16:03:23 浏览:934
直接脚本 发布:2024-09-19 15:49:09 浏览:239
安卓代码怎么用 发布:2024-09-19 15:35:38 浏览:384
微信为什么要用服务器 发布:2024-09-19 15:35:29 浏览:882
东芝硬盘加密 发布:2024-09-19 15:29:44 浏览:138
天刀演奏脚本那个好用 发布:2024-09-19 15:29:43 浏览:97
sql列号查询 发布:2024-09-19 14:58:07 浏览:433
华为watchfit如何连接安卓手机 发布:2024-09-19 14:36:47 浏览:977
c数据库引用 发布:2024-09-19 14:26:03 浏览:230
armlinux内核源码剖析pdf 发布:2024-09-19 14:25:03 浏览:807