代码之家  ›  专栏  ›  技术社区  ›  PNC

使用python捕捉屏幕中心的图像

  •  -2
  • PNC  · 技术社区  · 8 年前

    如何使用python捕捉屏幕中心我尝试了这段代码,但每当我将参数值提高到200以上时,就会出现错误。

    import numpy as np
    from PIL import ImageGrab
    import cv2
    
    while(True):
        printscreen_pil =  ImageGrab.grab(bbox=(0,200,500,200))
        printscreen_numpy =   np.array(printscreen_pil.getdata(),dtype='uint8')\
        .reshape((printscreen_pil.size[1],printscreen_pil.size[0],3)) 
        cv2.imshow('window',printscreen_numpy)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Dalen    8 年前

    你得到任何东西都是一种幻想。 两个Y值相同。目前,您的边界框为零像素高。一、 你有一条从(0200)到(500200)的线。(如果可以称为线路)。

    在代码中输入以下行:

    print printscreen_pil.size
    

    你会看到的。

    边界框由矩形的左上角和同一矩形的右下角描述。

    例如,您的边界框应该是:

    from PIL import ImageGrab
    x1 = 0; y1 = 200
    x2 = 500; y2 = 500
    # If you want center of a screen then calculate them according to the screen's size.
    img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
    

    屏幕大小可以直接从操作系统获取,也可以先获取整个屏幕,然后裁剪出所需的区域。

    img = ImageGrab.grab()
    size = img.size
    x1 = size[0]/2-100; y1= size[1]/2-100
    x2 = x1+200; y2 = y1+200
    portion = img.crop((x1, y1, x2, y2))