Skip to content Skip to sidebar Skip to footer

Adding JPG Images To Folium Popup

I try to add an image to folium popup, but failed. I use python 2.7 version, and folium 0.50 version. Actually, I follow the page mention in other threads, but it still doesn't wor

Solution 1:

 import base64
 from folium import IFrame

 #Add Marker
 encoded = base64.b64encode(open('mypict.jpg', 'rb').read())
 html = '<img src="data:image/png;base64,{}">'.format
 iframe = IFrame(html(encoded.decode('UTF-8')), width=400, height=350)
 popup = folium.Popup(iframe, max_width=400)

 folium.Marker(location=[43.591545, 39.728056], tooltip=html, popup = popup, 
 icon=folium.Icon(color = 'gray')).add_to(map)

Solution 2:

I've followed this example and it (almost) worked for me. Plots were not base64-decoded correctly because the encoded variable was a byte array instead of a string, thus producing a b'iVBOR header instead of a iVBOR header (the base64 version of the PNG header).

Replacing html(encoded) to html(encoded.decode('UTF-8')) fixed the problem.

Here is the output.

Folium Heatmap with Graph


And this is the code snippet.

    fig, ax = plt.subplots(figsize=(width, height))
    ax = subdf.plot(x='date', y='temperature', ax=ax, legend=False)
    ax.set_ylabel('Temp (°C)')
    png = '/tmp/temperatures_{}.png'.format(counter)
    fig.savefig(png, dpi=resolution)

    encoded = base64.b64encode(open(png, 'rb').read())



    html = '<img src="data:image/png;base64,{}">'.format
    #print(20*'-',encoded.decode('UTF-8'))
    iframe = IFrame(html(encoded.decode('UTF-8')), width=(width*resolution)+20, height=(height*resolution)+20)
    popup = folium.Popup(iframe, max_width=2650)

    icon = folium.Icon(color="red", icon="ok")
    marker = folium.Marker([lat, lon], popup=popup, icon=icon)
    marker.add_to(marker_cluster)

Post a Comment for "Adding JPG Images To Folium Popup"