gpt4 book ai didi

python - 如何在 python 中保存来自 selenium 和 opencv 的部分屏幕截图

转载 作者:行者123 更新时间:2023-12-02 16:21:17 24 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 和 selenium 将 html 中的特定元素保存为图像文件。但无法保存文件。

from selenium import webdriver
import cv2
import numpy as np


browser = webdriver.Firefox()
browser.get(<URl with image>)

# Element to be saved
element = browser.find_element_by_id(<id of element>)

png = browser.get_screenshot_as_png()
location = element.location
size = element.size

nparr = np.frombuffer(png, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)


left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']

im = img[left:right, top:bottom]

cv2.imwrite('filename.png',im)

当前运行此脚本,'filename.png' 中没有图像数据。

最佳答案

正如@Zdar 所提到的,我正在编写以下解决方案:

from selenium import webdriver
import cv2
import numpy as np


browser = webdriver.Firefox()
browser.get(<URl with image>)

# Element to be saved
element = browser.find_element_by_id(<id of element>)

png = browser.get_screenshot_as_png()
location = element.location
size = element.size

nparr = np.frombuffer(png, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)


left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']

#im = img[left:right, top:bottom]
im = img[top:int(bottom), left:int(right)]
cv2.imwrite('filename.png',im)

关于python - 如何在 python 中保存来自 selenium 和 opencv 的部分屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50849686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com