【发布时间】:2021-12-15 09:33:13
【问题描述】:
我目前正在编写一个脚本来从 jpg 图像文件夹中提取 EXIF GPS 数据。我正在使用 os.scandir 从文件夹中提取条目,但据我了解,os.scandir 以任意方式打开文件。我需要按文件名按时间顺序打开图像。下面是我当前的代码,它按预期工作,但它没有以正确的顺序打开图像。我的图像文件夹中的文件按时间顺序命名,如下所示:“IMG_0097、IMG_0098”等。
#!/usr/bin/python
import os, exif, folium
def convert_lat(coordinates, ref):
latCoords = coordinates[0] + coordinates[1] / 60 + coordinates[2] / 3600
if ref == 'W' or ref == 'S':
latCoords = -latCoords
return latCoords
coordList=[]
map = folium.Map(location=[51.50197125069916, -0.14000860301423912], zoom_start = 16)
from exif import Image
with os.scandir('gps/') as entries:
try:
for entry in entries:
img_path = 'gps/'+entry.name
with open (img_path, 'rb') as src:
img = Image(src)
if img.has_exif:
latCoords = (convert_lat(img.gps_latitude, img.gps_latitude_ref))
longCoords = (convert_lat(img.gps_longitude, img.gps_longitude_ref))
coord = [latCoords, longCoords]
coordList.append(coord)
folium.Marker(coord, popup=str(coord)).add_to(map)
folium.PolyLine(coordList, color =" red", weight=2.5, opacity=1).add_to(map)
print(img_path)
print(coord)
else:
print (src.name,'has no EXIF information')
except:
print(img_path)
print("error occured")
map.save(outfile='/home/jamesdean/Desktop/Python scripts/map.html')
print ("Map generated successfully")
【问题讨论】: