我设置了一个 VirtualBox 来使用两个监视器。我尝试在第二台显示器上截取一个窗口:
import ImageGrab
im = ImageGrab.grab(windowrect)
im.save("img.png")
windowrect
我验证是窗口的正确矩形,在这种情况下,(1616, 2, 2594, 732)
. 然而,img.png
只是一个大黑匣子。任何想法如何解决屏幕抓取和 VirtualBox 之间的这种交互,以便我可以截取第二个虚拟监视器的屏幕截图?
我设置了一个 VirtualBox 来使用两个监视器。我尝试在第二台显示器上截取一个窗口:
import ImageGrab
im = ImageGrab.grab(windowrect)
im.save("img.png")
windowrect
我验证是窗口的正确矩形,在这种情况下,(1616, 2, 2594, 732)
. 然而,img.png
只是一个大黑匣子。任何想法如何解决屏幕抓取和 VirtualBox 之间的这种交互,以便我可以截取第二个虚拟监视器的屏幕截图?
我也遇到过同样的问题。如果我不得不猜测,我会假设ImageGrab
它从 SM_SCREEN 标志中获取它的坐标,它只给出主显示器的坐标,而不是 SM_VIRTUALSCREEN,它给出了整个虚拟屏幕(我没有查看源虽然,所以只是一个猜测)。
也就是说,通过直接与 Windows API 交互,然后将其位图输出转换为更可用的 PIL 对象,很容易解决这个问题。
def _get_screen_buffer(self, bounds=None):
# Grabs a DC to the entire virtual screen, but only copies to
# the bitmap the the rect defined by the user.
SM_XVIRTUALSCREEN = 76 # coordinates for the left side of the virtual screen.
SM_YVIRTUALSCREEN = 77 # coordinates for the right side of the virtual screen.
SM_CXVIRTUALSCREEN = 78 # width of the virtual screen
SM_CYVIRTUALSCREEN = 79 # height of the virtual screen
hDesktopWnd = windll.user32.GetDesktopWindow() #Entire virtual Screen
left = windll.user32.GetSystemMetrics(SM_XVIRTUALSCREEN)
top = windll.user32.GetSystemMetrics(SM_YVIRTUALSCREEN)
width = windll.user32.GetSystemMetrics(SM_CXVIRTUALSCREEN)
height = windll.user32.GetSystemMetrics(SM_CYVIRTUALSCREEN)
if bounds:
left, top, right, bottom = bounds
width = right - left
height = bottom - top
hDesktopDC = windll.user32.GetWindowDC(hDesktopWnd)
if not hDesktopDC: print 'GetDC Failed'; sys.exit()
hCaptureDC = windll.gdi32.CreateCompatibleDC(hDesktopDC)
if not hCaptureDC: print 'CreateCompatibleBitmap Failed'; sys.exit()
hCaptureBitmap = windll.gdi32.CreateCompatibleBitmap(hDesktopDC, width, height)
if not hCaptureBitmap: print 'CreateCompatibleBitmap Failed'; sys.exit()
windll.gdi32.SelectObject(hCaptureDC, hCaptureBitmap)
SRCCOPY = 0x00CC0020
windll.gdi32.BitBlt(
hCaptureDC,
0, 0,
width, height,
hDesktopDC,
left, top,
0x00CC0020
)
return hCaptureBitmap
def _make_image_from_buffer(self, hCaptureBitmap):
import Image
bmp_info = BITMAPINFO()
bmp_header = BITMAPFILEHEADER()
hdc = windll.user32.GetDC(None)
bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
DIB_RGB_COLORS = 0
windll.gdi32.GetDIBits(hdc,
hCaptureBitmap,
0,0,
None, byref(bmp_info),
DIB_RGB_COLORS
)
bmp_info.bmiHeader.biSizeImage = bmp_info.bmiHeader.biWidth *abs(bmp_info.bmiHeader.biHeight) * (bmp_info.bmiHeader.biBitCount+7)/8;
size = (bmp_info.bmiHeader.biWidth, bmp_info.bmiHeader.biHeight )
print size
pBuf = (c_char * bmp_info.bmiHeader.biSizeImage)()
windll.gdi32.GetBitmapBits(hCaptureBitmap, bmp_info.bmiHeader.biSizeImage, pBuf)
return Image.frombuffer('RGB', size, pBuf, 'raw', 'BGRX', 0, 1)
第一个函数获取屏幕的位图,第二个函数将其转换为 PIL 对象。
如果您不想自己计算其他显示器的坐标,我有一个名为PyRobot的小模块,它具有针对特定显示器等的功能。而且它是纯 Python,所以不需要安装 PyWin32 :)
音频/ chriskiehl !
我拿了你的帖子和你的一些图书馆,并为 python 3 重新编写了它!我希望你没事。它适用于我使用 PILLOW (PIL) 和 python 3.4 引擎。
我刚刚调用了文件duelMonitory.py,其中包含:
# https://github.com/chriskiehl/pyrobot
# started from Audionautics code on http://stackoverflow.com/questions/3585293/pil-imagegrab-fails-on-2nd-virtual-monitor-of-virtualbox
# updated for PILLOW and Python 3 by Alan Baines (Kizrak)
from PIL import *
#from ctypes import windll, Structure, byref, c_uint
#import ctypes
import ctypes
from ctypes import *
from ctypes.wintypes import *
def get_screen_buffer(bounds=None):
# Grabs a DC to the entire virtual screen, but only copies to
# the bitmap the the rect defined by the user.
SM_XVIRTUALSCREEN = 76 # coordinates for the left side of the virtual screen.
SM_YVIRTUALSCREEN = 77 # coordinates for the right side of the virtual screen.
SM_CXVIRTUALSCREEN = 78 # width of the virtual screen
SM_CYVIRTUALSCREEN = 79 # height of the virtual screen
hDesktopWnd = windll.user32.GetDesktopWindow() #Entire virtual Screen
left = windll.user32.GetSystemMetrics(SM_XVIRTUALSCREEN)
top = windll.user32.GetSystemMetrics(SM_YVIRTUALSCREEN)
width = windll.user32.GetSystemMetrics(SM_CXVIRTUALSCREEN)
height = windll.user32.GetSystemMetrics(SM_CYVIRTUALSCREEN)
if bounds:
left, top, right, bottom = bounds
width = right - left
height = bottom - top
hDesktopDC = windll.user32.GetWindowDC(hDesktopWnd)
if not hDesktopDC:
print ('GetDC Failed')
sys.exit()
hCaptureDC = windll.gdi32.CreateCompatibleDC(hDesktopDC)
if not hCaptureDC:
print ('CreateCompatibleBitmap Failed')
sys.exit()
hCaptureBitmap = windll.gdi32.CreateCompatibleBitmap(hDesktopDC, width, height)
if not hCaptureBitmap:
print ('CreateCompatibleBitmap Failed')
sys.exit()
windll.gdi32.SelectObject(hCaptureDC, hCaptureBitmap)
SRCCOPY = 0x00CC0020
windll.gdi32.BitBlt(
hCaptureDC,
0, 0,
width, height,
hDesktopDC,
left, top,
0x00CC0020
)
return hCaptureBitmap
def make_image_from_buffer(hCaptureBitmap):
from PIL import Image
bmp_info = BITMAPINFO()
bmp_header = BITMAPFILEHEADER()
hdc = windll.user32.GetDC(None)
bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
DIB_RGB_COLORS = 0
windll.gdi32.GetDIBits(hdc,
hCaptureBitmap,
0,0,
None, byref(bmp_info),
DIB_RGB_COLORS
)
bmp_info.bmiHeader.biSizeImage = int( bmp_info.bmiHeader.biWidth *abs(bmp_info.bmiHeader.biHeight) * (bmp_info.bmiHeader.biBitCount+7)/8 );
size = (bmp_info.bmiHeader.biWidth, bmp_info.bmiHeader.biHeight )
print (size)
pBuf = (c_char * bmp_info.bmiHeader.biSizeImage)()
windll.gdi32.GetBitmapBits(hCaptureBitmap, bmp_info.bmiHeader.biSizeImage, pBuf)
return Image.frombuffer('RGB', size, pBuf, 'raw', 'BGRX', 0, 1)
class BITMAPFILEHEADER(ctypes.Structure):
_fields_ = [
('bfType', ctypes.c_short),
('bfSize', ctypes.c_uint32),
('bfReserved1', ctypes.c_short),
('bfReserved2', ctypes.c_short),
('bfOffBits', ctypes.c_uint32)
]
class BITMAPINFOHEADER(ctypes.Structure):
_fields_ = [
('biSize', ctypes.c_uint32),
('biWidth', ctypes.c_int),
('biHeight', ctypes.c_int),
('biPlanes', ctypes.c_short),
('biBitCount', ctypes.c_short),
('biCompression', ctypes.c_uint32),
('biSizeImage', ctypes.c_uint32),
('biXPelsPerMeter', ctypes.c_long),
('biYPelsPerMeter', ctypes.c_long),
('biClrUsed', ctypes.c_uint32),
('biClrImportant', ctypes.c_uint32)
]
class BITMAPINFO(ctypes.Structure):
_fields_ = [
('bmiHeader', BITMAPINFOHEADER),
('bmiColors', ctypes.c_ulong * 3)
]
我的测试代码是duelMonitor.test.py,其中包含:
from PIL import *
from duelMonitor import *
hCaptureBitmap = get_screen_buffer()
pimage = make_image_from_buffer(hCaptureBitmap)
pimage.save("Hello.png","PNG")
基本上只是将 Audionautics 帖子和他的来自https://github.com/chriskiehl/pyrobot网页的库结合起来,并转换为 Python 3(和 PILLOW)。
玩得开心!
(PS。我会发表评论,但我没有足够的积分/sadface)