Lumbertumber
Lumbertumber

Reputation: 13

Streaming frames from the screen, generating a video

If I want to refresh the image I have to keep closing the window to refresh it. Is it possible without keep closing the window? Here is the code:

import numpy
import cv2
from PIL import ImageGrab
while True:

    img = ImageGrab.grab(bbox=(765,155,1135,195))
    img_np = numpy.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
    cv2.imshow("frame",frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Upvotes: 1

Views: 300

Answers (1)

Rotem
Rotem

Reputation: 32094

cv2.waitKey(0) waits without timeout, replace it with cv2.waitKey(time_in_msec).

Example for refreshing at about 10Hz:

import numpy
import cv2
from PIL import ImageGrab

while True:
    img = ImageGrab.grab(bbox=(765,155,1135,195))
    img_np = numpy.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
    cv2.imshow("frame",frame)
    cv2.waitKey(100)

cv2.destroyAllWindows()

See waitKey dcomentation:

imshow
Displays an image in the specified window.

Note This function should be followed by waitKey function which displays the image for specified milliseconds.
Otherwise, it won’t display the image.
For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display).
waitKey(25) will display a frame for 25 ms, after which display will be automatically closed.

Upvotes: 1

Related Questions