首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用win32gui捕获灰度屏幕

使用win32gui捕获灰度屏幕
EN

Stack Overflow用户
提问于 2021-08-26 10:09:30
回答 1查看 54关注 0票数 0

我使用以下代码在python中捕获窗口屏幕:

代码语言:javascript
复制
def get_screenshot(self):

    # get the window image data
    wDC = win32gui.GetWindowDC(self.hwnd)
    dcObj = win32ui.CreateDCFromHandle(wDC)
    cDC = dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
    cDC.SelectObject(dataBitMap)
    cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
    
    # convert the raw data into a format opencv can read
    #dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
    signedIntsArray = dataBitMap.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (self.h, self.w, 4)


    # free resources
    dcObj.DeleteDC()
    cDC.DeleteDC()
    win32gui.ReleaseDC(self.hwnd, wDC)
    win32gui.DeleteObject(dataBitMap.GetHandle())

    # drop the alpha channel, or cv.matchTemplate() will throw an error like:
    #   error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() 
    #   && _img.dims() <= 2 in function 'cv::matchTemplate'
    img = img[...,:3]

    # make image C_CONTIGUOUS to avoid errors that look like:
    #   File ... in draw_rectangles
    #   TypeError: an integer is required (got type tuple)
    # see the discussion here:
    # https://github.com/opencv/opencv/issues/14866#issuecomment-580207109
    img = np.ascontiguousarray(img)
    return img

我取了一些YouTube教程中的代码,代码运行得很好。

然而,我正在尝试将该图像转换为灰度,这样我以后就可以将其用于单应性,但没有任何成功。

我试着使用像cv.cvtColor这样的东西,但都不起作用。

有没有办法让它在捕获时变得灰度化?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 12:17:32

使用枕头包img.convert()函数转换为灰色样式

代码语言:javascript
复制
from PIL import Image

img = Image.open('download.png')
imgGray = img.convert('L')
imgGray.save('test_gray.png')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68936571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档