PIL ImageGrab在VirtualBox的第二个虚拟监视器上失败

时间:2010-08-27 14:41:52

标签: python image screenshot python-imaging-library virtualbox

我设置了一个VirtualBox来使用两个显示器。我尝试在第二台显示器上截取一个窗口的屏幕截图:

import ImageGrab
im = ImageGrab.grab(windowrect)
im.save("img.png")

windowrect我确认是窗口的正确矩形,在本例中为(1616, 2, 2594, 732)。但是,img.png只是一个大黑盒子。任何想法如何解决屏幕抓取和VirtualBox之间的这种交互,以便我可以截取第二个虚拟监视器的屏幕截图?

2 个答案:

答案 0 :(得分:2)

我遇到过同样的问题。如果我不得不猜测,我假设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 :)

答案 1 :(得分:1)

Audionautics / 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。我会发表评论,但我没有足够的分数/悲伤面)