import cv2
img = cv2.imread("D:/111.png")
cv2.imshow("Hello", img)
cv2.waitKey(0)
cv2.destoryAllWindows()
# 报错如下
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
主要是因为图片模式为P,转换之后就能正常打开显示了
import cv2
from PIL import Image
img = Image.open("D:/111.png")
# 打印图片模式,为P
print(img.mode)
# 转换图片模式
img = img.convert('RGB')
# 转换为np数组
img = np.asarray(img)
# 展示图片
cv2.imshow("Hello", img)
cv2.waitKey(0)
cv2.destoryAllWindows()