我正在尝试使用主机中运行的 python 脚本从我的来宾 Virtualbox 会话中截取屏幕截图。根据我的研究,最好的方法是访问 VirtualBox API 并使用 COM 常量来制作屏幕截图。但是,每次我尝试运行脚本时都会收到属性错误:
Traceback (most recent call last):
File "D:\computer_vision_scripts\take_screenshot.py", line 29, in <module>
take_screenshot('Windows 10 Pro')
File "D:\computer_vision_scripts\take_screenshot.py", line 16, in take_screenshot
machine.LockMachine(session, constants.LockType_Shared)
File "C:\Users\me\AppData\Roaming\Python\Python39\site-packages\win32com\client\__init__.py", line 180, in __getattr__
raise AttributeError(a)
AttributeError: LockType_Shared
我使用的脚本来自https://floatingoctothorpe.uk/2018/automating-virtualbox-screenshots-with-python.html
在文件版本中看起来像这样:
import win32com.client
from win32com.client import constants
def take_screenshot(vm_name, screenshot_path='screenshot.png'):
"""Create a VM Screenshot for a given VM"""
vbox = win32com.client.Dispatch("VirtualBox.VirtualBox")
session = win32com.client.Dispatch("VirtualBox.Session")
machine = vbox.FindMachine(vm_name)
machine.LockMachine(session, constants.LockType_Shared)
display = session.Console.Display
width, height, _, _, _, _ = display.GetScreenResolution(0)
screenshot = display.TakeScreenShotToArray(0, width, height,
constants.BitmapFormat_PNG)
session.UnlockMachine()
with open(screenshot_path, 'wb') as output_png:
output_png.write(screenshot.tobytes())
if __name__ == '__main__':
take_screenshot('Windows 10 Pro')
有谁知道我应该做什么才能让它发挥作用?
我也遇到了同样的问题。 像这样解决了:
添加此:
import virtualbox
from virtualbox import library
改变
machine.LockMachine(session, constants.LockType_Shared)
由
machine.LockMachine(session, virtualbox.library.LockType.shared)
和
screenshot = display.TakeScreenShotToArray(0, width, height,constants.BitmapFormat_PNG)
由
screenshot = display.TakeScreenShotToArray(0, width, height, virtualbox.library.BitmapFormat.png)
在这里找到了这个解决方案: https://github.com/sethmlarson/virtualbox-python/blob/master/tests/test_test_vm.py
我尝试使用 selenium webdriver pyhton 来获取屏幕,我在服务器中部署了代码,但没有任何监视器,所以在 docker 中尝试使用此功能,例如虚拟 diaply def screen_shot(自身,client_id,驱动程序): 截图文件名=“截图.png” driver.save_screenshot(屏幕截图文件名) # 将图片上传到Imgur 将 open("screenshot.png", "rb") 作为 f: 响应 = requests.post( “https://api.imgur.com/3/upload”, headers={"授权": f"客户端 ID {client_id}"}, 文件={“图像”:f}, ) 数据=response.json()
if response.status_code == 200:
imgur_url = data["data"]["link"]
return imgur_url
else:
print("Image upload failed.")
cant able to take screen shot in the server side is there any better to take screen shot and convert to url