# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import piexif
import skimage.io as io
import csv
import math
#读取pos信息
exampleFile = open('实验数据.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
del exampleData[0]
#print(exampleData)
print('*'*80)
#读取所有的图像
string='*.jpg'
s = 'data\\'
coll = io.ImageCollection(s+string)
#print(str)
# print(coll.files)
#print(type(coll.files))
#print(len(coll))
# io.imshow(coll[1])
#将pos信息写入图像
for i in range(len(coll)):
if coll.files[i][5:] == exampleData[i][0]:
# print(exampleData[i][0])
exif_dict = piexif.load(coll.files[i])
#print(exampleData[i][1:3])
a2,a1 = math.modf(float(exampleData[i][2]))
a1 = int(a1)
a3,a2 = math.modf(a2*60)
a2 = int(a2)
a4,a3 = math.modf(a3*60*1000000)
a3 = int(a3)
#print(a3,a2,a1)
exif_dict['GPS'][2] = ((a1, 1), (a2, 1), (a3, 1000000))
b2,b1 = math.modf(float(exampleData[i][1]))
b1 = int(b1)
b3,b2 = math.modf(b2*60)
b2 =int(b2)
b4,b3 = math.modf(b3*60*1000000)
b3 = int(b3)
exif_dict['GPS'][4] = ((b1, 1), (b2, 1), (b3, 1000000))
#print(float(exampleData[i][3])*1000000)
h = round(float(exampleData[i][3])*10000000)
exif_dict['GPS'][6] = [h, 10000000]
exif_bytes = piexif.dump(exif_dict)
io.imsave('result\\'+coll.files[i][5:],coll[i],exif= exif_bytes)
print('%s已添加pos'%coll.files[i][5:])
# print(coll.files[i])
print('图片已全部添加pos')
# print(exif_dict['GPS'][2])
# print(exif_dict['GPS'][4])
# print(exif_dict['GPS'][6])