Python ipyleaflet将地图导出为PNG或JPG或SVG

MBU*_*ser 5 python export image leaflet

我尝试将ipyleaflet数据可视化导出为PNG或任何其他文件格式,但是我找不到有效的方法。例如,在大叶中有map.save(path)。我的研究中缺少ipyleaflet中的图书馆或方法,可以帮助我实现自己的目标吗?

这是一些生成地图的示例代码

from ipyleaflet import *
center = [34.6252978589571, -77.34580993652344]
zoom = 10
m = Map(default_tiles=TileLayer(opacity=1.0), center=center, zoom=zoom)
m
Run Code Online (Sandbox Code Playgroud)

我想将此地图导出为图像文件,而无需手动截图。

我找到了两个可以导出javascript传单地图的资源:https : //github.com/aratcliffe/Leaflet.printhttps://github.com/mapbox/leaflet-image

不幸的是,我无法在python中使用它们。

Maa*_*els 6

为了生成 html,您可以使用ipywidgets

from ipywidgets.embed import embed_minimal_html
embed_minimal_html('map.html', views=[m])
Run Code Online (Sandbox Code Playgroud)

如果你想制作PNG,你可以使用ipywebrtc,更具体地说:

或者在代码中:

from ipywebrtc import WidgetStream, ImageRecorder
widget_stream = WidgetStream(widget=m, max_fps=1)
image_recorder = ImageRecorder(stream=widget_stream)
display(image_recorder)
Run Code Online (Sandbox Code Playgroud)

保存 PNG:

with open('map.png', 'wb') as f:
    f.write(image_recorder.image.value)
Run Code Online (Sandbox Code Playgroud)

或者转换为枕头图像进行预处理:

import PIL.Image
import io
im = PIL.Image.open(io.BytesIO(image_recorder.image.value))
Run Code Online (Sandbox Code Playgroud)

  • 我尝试使用它,但得到一个空的 map.png 文件 (3认同)

MBU*_*ser 5

我和我的同事在ipyleaflet(python)图像导出方面找到了不错的工作。下面是它的工作原理。导出需要叶草库。此示例中的GeoJson数据已使用样式属性进行了准备:

import folium
map = folium.Map([51., 12.], zoom_start=6,control_scale=True)
folium.GeoJson(data).add_to(map)
map.save('map.html')
Run Code Online (Sandbox Code Playgroud)

结果是这样的: 输出

可以使用子进程调用在python(windows)中进一步处理html文件,从而从中生成PDF或PNG。我希望这会有所帮助,因为python的ipyleaflet文档几乎不存在。