我想读取窗口屏幕并使用cv2.imshow()方法显示它。
现在,我正在获取窗口的ScreenShot,并将其显示在OpenCV窗口上,但它也显示了我不想要的自身。
我应该采取其他哪种方法来获得我的结果?
这是我现在使用的代码。
while True:
img = screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("Test", img)我正在使用的图书馆是:
这就是我不想发生的事。保存的截图
https://i.stack.imgur.com/7PaC1.jpg
代码是采取截图的倒影窗口,但我也不想关闭或最小化的不显示窗口。
问:有什么其他方法来实现我想要的吗?
发布于 2021-07-25 21:35:27
我个人的偏好是使用cv2.imwrite而不是cv2.imshow。但是,如果您的需求需要您使用imshow,您可以检查这两个方法,看看哪些适合您的需求。
选项1:在截图之前销毁窗口,然后再做一次,其代码如下所示:
while True:
img = screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("Test", img)
cv2.destroyAllWindows()我个人认为这种方法没有很大的优点,因为它主要是不断地创建和销毁窗口。
选项2: OpenCV还允许您移动窗口,您可以使用它在准备截图之前移动窗口,然后再将其移回。相同的代码如下所示:
while True:
# Just to check if img exists or not, needed for the 1st run of the loop
if 'img' in locals():
cv2.waitKey(100) #Without the delay, the imshow window will only keep flickering
cv2.moveWindow("Test", height, width)
img = screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width, ch = img.shape
cv2.imshow("Test", img)
cv2.moveWindow("Test", 0, 0)上面的两个选项使用的是您已经在代码中使用的库。还有第三个选项,您可以最小化窗口,然后每次拍摄截图时都重新打开它。您可以在这里和这里上找到对它的引用。同样的代码应该是。
import ctypes
import win32gui
while True:
# Just to check if img exists or not,
# needed for the 1st run of the loop
if 'img' in locals():
cv2.waitKey(500) # Delay to stop the program from constantly opening and closing the window after itself
ctypes.windll.user32.ShowWindow(hwnd, 7)
# Window needs some time to be minimised
cv2.waitKey(500)
img = pyautogui.screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("Test", img)
hwnd = win32gui.GetForegroundWindow()
ctypes.windll.user32.ShowWindow(hwnd, 9)https://stackoverflow.com/questions/68489602
复制相似问题