Export a folium map as a png

Mittenchops picture Mittenchops · Nov 30, 2018 · Viewed 13.8k times · Source

I'm working with a map created using python, folium, and geojson, similar to this one.

However, instead of this image being an interactive HTML document, I would rather simply export it to png or svg.

Using the syntax:

m = folium.Map( # etc..)
m.save("filename.png")

Saves a file, but it is still HTML, rather than png. What's the correct output command to render not-to-html?

Answer

Michel Metran picture Michel Metran · Jun 18, 2020

I use this:

... where m is my map object. And 5 is the time (seconds) to render the map.

import io
from PIL import Image

img_data = m._to_png(5)
img = Image.open(io.BytesIO(img_data))
img.save('image.png')