dv akhil
dv akhil

Reputation: 93

python importing all images from a local directory

import cv2
import os


for filename in os.listdir('C:/Users/Akhil/Downloads/New'):
     image = cv2.imread(filename)
      gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      cv2.imwrite('gray_image.png',gray_image)
      cv2.imshow('color_image',image)
      cv2.imshow('gray_image',gray_image) 
      cv2.waitKey(0)                 
     cv2.destroyAllWindows()

I am importing all the images from a directory as shown above and turning each image into a grey scale image. But when I run this code, I am facing the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::ipp_cvtColor, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7456
Traceback (most recent call last):
  File "11.py", line 7, in <module>
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function cv::ipp_cvtColor

Upvotes: 0

Views: 1092

Answers (1)

Jacob Panikulam
Jacob Panikulam

Reputation: 1218

This is often a symptom of failing to find the image.

Note that the filenames returned by os.listdir() are relative to the directory passed to listdir(). So OpenCV is searching for a file named "image_01.jpg" for example, in your current working directory, and can't find anything. But instead of failing, it returns an empty image.

Try path_to_image = os.path.join(my_image_folder, filename) and imread that.

Upvotes: 1

Related Questions