简体   繁体   中英

OpenCV Python Assertion Failed Error imread

The code opens the image when I reference the file within the same directory as the script.

import cv2

image = cv2.imread("art.jpg")
cv2.imshow("original", image)
cv2.waitKey(0)

I only get the Assertion Failed error whenever I'm attempting to open the same file from one directory deeper.

import cv2
import os
import random

dir = 'images'
filename = random.choice(os.listdir("images"))
print(filename)
path = os.path.join(dir, filename)
print(path)

image = cv2.imread("path")
cv2.imshow("original", image)
cv2.waitKey(0)

art.jpg

images/art.jpg

Traceback (most recent call last): File "slideShow.py", line 12, in cv2.imshow("original", image) cv2.error: OpenCV(3.4.2) /io/opencv/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Your mistake is here:

image = cv2.imread("path")

Your input is a string change it to

image = cv2.imread(path)

import cv2
import os
import random

dir = 'images'
filename = random.choice(os.listdir("images"))
print(filename)
path = os.path.join(dir, filename)
print(path)

image = cv2.imread(path)
cv2.imshow("original", image)
cv2.waitKey(0)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM