I am trying to display an image using open cv and used the following code (from geeksforgeeks). However, when I run from the terminal (with zsh in MacOS 11.6.1 using Python 3.7.5
and opencv-python==4.2.0.34
a python process is launched and nothing more happens (there is no error message and no image is appearing). What am I doing wrong?
import cv2
# Path to image in local directory
path = 'path/to/image.png'
# Using cv2.imread() to read an image in grayscale mode
image = cv2.imread(path, 0)
# Using namedWindow()
# A window with 'Display_Image' name is created
# with WINDOW_NORMAL allowing us to have random size
cv2.namedWindow("Display_Image", cv2.WINDOW_NORMAL)
# Using cv2.imshow() to display the image
cv2.imshow('Display_Image', image)
# Waiting 0ms for user to press any key
cv2.waitKey(0)
# Using cv2.destroyAllWindows() to destroy
# all created windows open on screen
cv2.destroyAllWindows()
Edit
I edited the code according to comment to print the size of the image as follows with the following output in the terminal : (480, 640)
. But still no image nor error message happens
import cv2
# Path to image in local directory
path = 'path/to/image.png'
# Using cv2.imread() to read an image in grayscale mode
image = cv2.imread(path, 0)
print(image.shape)
# Using namedWindow()
# A window with 'Display_Image' name is created
# with WINDOW_NORMAL allowing us to have random size
cv2.namedWindow("Display_Image", cv2.WINDOW_NORMAL)
# Using cv2.imshow() to display the image
cv2.imshow('Display_Image', image)
# Waiting 0ms for user to press any key
cv2.waitKey(0)
# Using cv2.destroyAllWindows() to destroy
# all created windows open on screen
cv2.destroyAllWindows()
CodePudding user response:
Ok after following the github thread there https://github.com/opencv/opencv-python/issues/423, I updated the package to the newest version (in my case opencv-python-4.6.0.66
) and it worked.