Ask Your Question
0

How to refresh the image content in a window?

asked 2018-01-06 09:55:55 -0600

little_bob gravatar image

updated 2018-01-06 09:56:24 -0600

cv2.namedWindow("image",0)

img=cv2.imread("autojump.png")
while 1:
    img=cv2.imread("autojump.png")
    cv2.imshow("image",img)
    k=cv2.waitKey(0) & 0XFF
    if k== 27 :
        break
cv2.destroyAllWindows()

The image file autojump.png's content will change along with time(rewrited by with open("autojump.png","w" as f : f.write(some_other_image_bytes_stream)), so I reload it during the while loop, but it seems that the window never refresh.Just always the same image. What's wrong?

edit retag flag offensive close merge delete

Comments

1

press a key or change cv2.waitKey(0) -> cv2.waitKey(10)

sturkmen gravatar imagesturkmen ( 2018-01-06 09:57:40 -0600 )edit

you should destory img first,then read a new "autojump.png"

jsxyhelu gravatar imagejsxyhelu ( 2018-01-07 16:37:00 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2018-01-08 03:44:12 -0600

updated 2018-01-08 03:45:11 -0600

Simply said, what @sturkmen says, is that you do not automatically refresh the window, but wait for a keypress. Your waitKey(0) holds your loop forever, until a key is pressed. This makes that your while loop is actually not doing anything. You can solve this by changing k=cv2.waitKey(0) & 0XFF to k=cv2.waitKey(10) & 0XFF which will auto refresh your screen each 10 milliseconds.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-06 09:53:28 -0600

Seen: 21,767 times

Last updated: Jan 08 '18