【问题标题】:how to save map containing markers into image file using phantomjs如何使用phantomjs将包含标记的地图保存到图像文件中
【发布时间】:2021-03-17 05:50:32
【问题描述】:

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

import folium
m_f = folium.Map(location= [39.76258, 254.994682],
                 zoom_start=8)

m_f.save('f_map.html')
m_f

然后我使用 phantomjs 保存图像。

import selenium.webdriver
import time

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='/Users/path/Downloads/PhantomJS/bin/phantomjs')

driver.set_window_size(4000, 3000)  # choose a resolution
driver.get('f_map.html')
# You may need to add time.sleep(seconds) here
time.sleep(1)
driver.save_screenshot('f_map.png')

到这里为止没有问题。
但是,当我在地图上添加一些标记时,我得到了正确的 html 文件,我也可以在浏览器中打开它,但它的屏幕截图是一个空白文件(内容)。

这是新地图的代码

import folium
m_f = folium.Map(location= [39.74258, 254.993682],
                 zoom_start=12)

arr_test1 = [39.74258, 254.993682]
arr_test2 = [39.75258, 254.994682]
arr_test3 = [39.76258, 254.994682]

all_loc = [arr_test1, arr_test2, arr_test3]

for cur_loc in all_loc:
    point = folium.Circle(
        radius=200,
        location=cur_loc,
        popup='The Waterfront',
        color='red',
        fill=False,
    )
    point.add_to(m_f)
    
    time.sleep(0.001)
    
m_f
m_f.save('f_map_marker.html')

及其phantonjs代码行

import selenium.webdriver
import time

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path='/Users/path/Downloads/PhantomJS/bin/phantomjs')

driver.set_window_size(4000, 3000)  # choose a resolution
driver.get('f_map_marker.html')

time.sleep(1)
driver.save_screenshot('f_map_marker.png')

所以,f_map_marker.png 文件是空白的。
有谁知道,为什么会这样,以及我该如何解决这个问题?
欢迎任何提示。

【问题讨论】:

    标签: python dictionary selenium-webdriver phantomjs folium


    【解决方案1】:

    我使用 Firefox (geckodriver) 而不是 PhantomJS 检查了您的代码,并且我在“f_map_marker.png”文件中的地图上有 3 个圆圈。也许是因为 PhantomJS 项目不再维护。您可以查看 PhantomJS 存储库上的 Github 评论:https://github.com/ariya/phantomjs/issues/15344

    【讨论】:

    • 谢谢@Pierre-Loic,我会检查的。
    • 请您看看这个link 好吗?我已经创建了一个类似的问题,我尝试从 html 文件中制作图像。
    • pdfkit 仅适用于创建 pdf。我认为您的脚本中有错字:您需要使用 imgkit 将图像作为输出
    • 根据 [doc.] (pypi.org/project/imgkit) 这应该可以工作:import imgkit and imgkit.from_file('f_map.html', 'outpdf.pdf') 。但我收到一个错误:OSError: No wkhtmltoimage executable found: "b''" 。我想这是因为,我没有精确到wkhtmltopdf 的路径。我怎么能那样做?任何想法 ?如果是 pdf,config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf') 是解决方案。
    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多