首页 > 解决方案 > Python:将JPG图像添加到folium中的弹出窗口

问题描述

我正在尝试使用 folium 将 jpg 图像添加到地图上的标记弹出窗口。到目前为止,我有一个程序可以扫描图像文件夹并按时间顺序在地图上绘制它们的位置。我已经阅读了这里的一些其他线程,并且我试图实现代码,但是那些处理的是单一图像而不是它们的文件夹。

我需要能够在地图上的每个标记上预览文件夹中的所有 jpg 图像。

当我运行代码时,我遇到了这个错误: FileNotFoundError: [Errno 2] No such file or directory: 'IMG_0097.jpg'

任何帮助表示赞赏,谢谢。

import os, exif, folium, base64

def convert_lat(coordinates, ref):
    latCoords = coordinates[0] + coordinates[1] / 60 + coordinates[2] / 3600
    if ref == 'W' or ref == 'S':
        latCoords = -latCoords
    return latCoords
filename=[]
coordList=[]
map = folium.Map(location=[51.50197125069916, -0.14000860301423912], zoom_start = 16)

from exif import Image
with os.scandir('gps/') as entries:
    for entry in entries:   
        filename= [i.name for i in entries if not i.name.startswith('.')]
        sortedList = sorted(filename, key=lambda x: int(x.split("_")[1].split(".")[0]))
        for c in range(len(sortedList)):
            img_path = ('gps/'+sortedList[c])           
            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)
                else:
                    print (src.name,'has no EXIF information')

count=0
for c in range(len(coordList)):
    popmsg = "Count is "+str(count+1)
    if count==0:
        fileimg=(sortedList[c])
        encoded = base64.b64encode(open(sortedList[c], 'rb').read())
        html= '<img src="data:image/jpg;base64,{}">'.format
        resolution, width, height = 75, 50, 25
        iframe = IFrame(html(encoded), width=(width*resolution)+20, height=(height*resolution)+20)
        popup = folium.Popup(iframe, max_width=1000)
        folium.Marker(coordList[c],popup=popup,icon=folium.Icon(icon='home',prefix='glyphicon')).add_to(map)
    elif count==(len(coordList)-1):
        folium.Marker(coordList[c],popup=popmsg,icon=folium.Icon(icon='flag',prefix='glyphicon')).add_to(map)
    else:
        folium.Marker(coordList[c],popup=popmsg,icon=folium.Icon(icon='asterisk',prefix='glyphicon')).add_to(map)
    count+= 1
        
        


folium.PolyLine(coordList, color =" red", weight=2.5, opacity=1).add_to(map)```


标签: pythonfolium

解决方案


推荐阅读