I'm running this basic code in python. It has no loops but it runs endelessly, I had the command to save an image, that i turned from colored to grayscaled.
So before the cv2.waitKey(0) the code works and I can save the first image, after cv2.waitKey(0) it reads nothing, because, I think, it continues looping.
this is the very basic code:
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
cwd = os.getcwd()
print(cwd)
img = cv2.imread("C:/Users/carlo/PycharmProjects/OpenCV_AnaConda/opencv/samples/data/lena.jpg",0)
print(img)
cv2.imwrite('C:/Users/carlo/PycharmProjects/OpenCV_lavendetta/lena_1.jpg', img)
print("it runs the code until here")
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('C:/Users/carlo/PycharmProjects/OpenCV_lavendetta/lena_2.jpg', img)
print("the code ends")
How can I solve this? Thank You
You mention looping, but there are no loops in your code. cv2.waitKey(0)
is going to wait infinitely for user input. From the docs:
The function waitKey waits for a key event infinitely (when delay <= 0 ) or for delay milliseconds, when it is positive.
waitKey(0)
is waiting for a key to be pressed, so you press a key and the program should continue. If you want it to only wait for a limited amount of time, enter the number of milliseconds you want to have it appear for.
For example: to have it only appear for 3 seconds and then continue program execution do cv2.waitkey(3000)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.