Python OpenCV cv2.WaitKey()和Windows 10

2024-04-29 03:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个小python程序来捕捉屏幕上的图像。当按下Esc或“a”键时,它被设计为停止。它正在按程序捕捉图像,但当我按下Esc或“a”键时不会停止。我的操作系统是Windows10,我使用的是Python3.6.4(Anaconda)。代码如下:

import cv2
import mss
from PIL import Image

#Creates an endless loop for high-speed image acquisition...
while 1:
    with mss.mss() as sct:
        #Get raw pixels from the screen
        sct_img = sct.grab(sct.monitors[1])

        #Create the Image
        img = Image.frombytes('RGB', sct_img.size, sct_img.bgra, 'raw', 'BGRX')

        #Show the Image
        cv2.imshow('test', np.array(img))

        #Kill-switch: press the 'Esc' or 'a' keys to exit the program
        if (cv2.waitKey(33) == 27) or (cv2.waitKey(33) == ord('a')):
            cv2.destroyAllWindows()
            break

我哪里出错了?在


Tags: orthefrom图像imageimport程序img