mercredi 20 octobre 2021

How to open a random image from specified folder/directory in Python OpenCV

I want my program to open a random image from the folder.

This works:

import cv2
import os
import random

capture = cv2.imread(".Images/IMG_3225.JPEG")

But when I want to do this random it doesn't work:

file = random.choice(os.listdir("./images/"))
capture = cv2.imread(file)

I'm getting the following error:

cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

What am I doing wrong??




Aucun commentaire:

Enregistrer un commentaire