python importing all images from a local directory
<pre><code>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()
</code></pre>

<p>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:</p>

<pre><code>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 &lt;module&gt;
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
</code></pre>

<p>This is often a symptom of failing to find the image. </p>

<p>Note that the <code>filename</code>s returned by <code>os.listdir()</code> 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. </p>

<p>Try <code>path_to_image = os.path.join(my_image_folder, filename)</code> and imread that.</p>

python opencv python-import