[英]How to convert html map into image (png or jpg )
我正在尝试将包含标记和热图的 map 保存到图像中。 这是显示 map 的代码。
from ipywidgets import Layout
import geopandas
defaultLayout=Layout(width='3000px', height='3000px') # A very large image.
lat_lgn = [39.74248, 254.993622]
m_f = Map(center=lat_lgn, zoom=12, layout=defaultLayout)
marker_m = Marker(location=lat_lgn, draggable=False)
m_f.add_layer(marker_m)
m_f
在上面添加一些标记
arr_test1 = [39.74258, 254.993682]
arr_test2 = [39.76288, 254.988932]
arr_test3 = [39.79998, 254.991982]
all_loc = [arr_test1, arr_test2, arr_test3]
for cur_loc in all_loc:
point = CircleMarker(
radius=10,
location=cur_loc,
color='red',
fill_color="black",
)
m_f.add_layer(point)
time.sleep(0.001)
将 map 保存为 html 文件。 工作正常。
m_f.save('f_map.html', title='My Map')
当我尝试从 html 获取图像或 pdf 时出现问题。
import imgkit
import pdfkit
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_file('f_map.html', 'outpdf.pdf', configuration=config)
pdfkit.from_file('f_map.html', 'outjpg.jpg', configuration=config)
pdfkit.from_file('f_map.html', 'outpng.png', configuration=config)
pdf 文件为空
并且 macBook 无法打开 jpeg 和 png 文件。
为了检查我的依赖关系,我试过这个:
import pdfkit
pdfkit.from_url('http://stackoverflow.com', 'out.pdf', configuration=config)
效果很好。 但是,一旦将out.pdf
更改为out.png
,就无法打开获取的文件。
有谁知道我该如何解决这个问题?
我正在努力获得巨大的形象。 但它也不适用于 300px X 300px 的图像。
欢迎任何提示。
一种选择(诚然有点粗糙)是使用 selenium 库自动打开 HTML 并从那里截取屏幕截图,然后将其另存为图像。 代码看起来像(我在这里根据 MS Edge 给出了代码,但其他浏览器的代码应该类似):
from selenium import webdriver
from time import sleep
filename = '<Name (without path) of your HTML file here>'
MyBrowser = webdriver.Edge(r"<path to webdriver here>\\msedge.exe")
murl = r'file:\\{path}\{mapfile}'.format(path='<path to HTML here>',mapfile=mfilename)
MyBrowser.get(murl)
sleep(10) #this is not strictly necessary but is here in case you need time for map tiles to load
MyBrowser.save_screenshot('<Image_file_name>')
MyBrowser.quit()
请注意,这将要求您提前为浏览器安装网络驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.