我想在草皮地图的标记中显示jpg图像作为弹出窗口。我找到了一些示例代码,但是当我使用同一目录中的图像运行它时,弹出窗口只是空白。地图和标记似乎创建良好,但是弹出窗口只是空白。
我确定我缺少明显的东西,有人可以指出吗?
import folium
import branca
import os
import json
import numpy as np
#import vincent
from folium import IFrame
import pandas as pd
import numpy.ma as ma
import base64
import matplotlib.pyplot as plt
def make_data():
x = np.linspace(-np.pi, np.pi, 101)
sin = np.sin(x)
cos = np.cos(x)
cos[20:50] = np.NaN
return pd.DataFrame(np.asanyarray([sin, cos]).T, columns= ['sin', 'cos'], index=x)
df = make_data()
resolution, width, height = 75, 7, 3
station = '42'
lon, lat = -42, -21
mapa = folium.Map(location=[lat, lon], zoom_start=5)
encoded = base64.b64encode(open('P8290065.JPG', 'rb').read()).decode()
html = '<img src="data:image/JPG;base64,{}">'.format
iframe = IFrame(html(encoded), width=632+20, height=420+20)
popup = folium.Popup(iframe, max_width=2650)
icon = folium.Icon(color="blue", icon="ok")
marker = folium.Marker(location=[lat-2, lon+1], popup=popup, icon=icon)
mapa.add_child(marker);
mapa.save("MapImage.html")
有同样的问题。应该是图像大小的问题。尝试:
from PIL import Image
image = Image.open(imagename)
image = image.resize((width, height), Image.ANTIALIAS)
image.save(resize_path + imagename, 'jpg', quality=100)
...
encoded = base64.b64encode(open('P8290065.JPG', 'rb').read())
html = '<img src="data:image/JPG;base64,{}">'.format
iframe = IFrame(html(encoded.decode("UTF-8")), width=632+20, height=420+20)
popup = folium.Popup(iframe, max_width=2650)
这对我有用。请注意,我从第一行中删除了编码,然后添加了
html(encoded.decode(“ UTF-8”))
到html行。