0

我正在尝试将包含标记的地图和热图保存到图像中。这是显示地图的代码。

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)

将地图保存为 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.pdfout.png,我无法打开获得的文件。
有谁知道,我该如何解决这个问题?
我试图获得巨大的形象。但它也不适用于 300 像素 X 300 像素的图像。
欢迎任何提示。

4

1 回答 1

0

One option (which admittedly is a little crude) is to use the selenium library to automatically open the HTML and from there take a screenshot which then is saved as an image. The code will look like (I have given the code here according to MS Edge, but the code should be similar for other browsers):

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()

Note that this will require you to install a webdriver for your browser in advance.

于 2020-12-06T10:54:03.027 回答