2017-10-08 72 views
-2

如何捕獲使用python我想這個代碼在屏幕的中心,但它給了我錯誤,每當我彌補了以上參數200圖像捕獲屏幕的中心使用python

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 
+0

將示例圖像添加到捕獲的問題和輸出中,以顯示屏幕圖像的中心。 OP應該明確地描述問題以避免任何猜測。 – thewaywewere

回答

1

它自己的價值是一個徘徊,你得到的任何東西。 你有兩個Y值相同。目前,你的邊界框是零像素高。即你有一條線從(0,200)到(500,200)。 (如果它可以被稱爲一條線)。

把你下面的一行代碼:

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))