I am trying to capture specific windows, and displaying them using ImageGrab using the following code:
import cv2
import numpy as np
from PIL import ImageGrab
import pygetwindow
import pyautogui
titles = pygetwindow.getAllTitles()
while(1):
cars = pygetwindow.getWindowsWithTitle('car - Google Search - Google Chrome')[0]
print(cars.left, cars.top ,cars.width, cars.height)
img = ImageGrab.grab(bbox=(int(cars.left),int(cars.top),cars.width,cars.height)) #x, y, w, h
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.CV_8U)
cv2.waitKey(1)
cv2.imshow("frame", frame)
cv2.destroyAllWindows()
It is somehow able to display Chrome, and able to follow the window, but I noticed that whether the x and y axis of the bbox is larger than the width and height, there will be an error. Furthermore, whenever I try to move the Chrome browser around, the frame's height and width key adjust itself. Any ideas on how to solve these issues?
The problem originates from your
ImageGrab.grabcall. Unfortunately, it's not mentioned in the documentation itself, but from the source code, you see, that it's:So, you must calculate
rightandbottomaccordingly. This code works perfectly fine for me:Please notice, I also corrected your
cv2.cvtColorcall, such that proper colors are displayed.