0

I am trying to import and read all images in a folder. However, when I have more than 5 images, cv2.imread returns none for the 6th image. I have tried using different file names, different files, etc, but I can't get it to work.

import cv2
import numpy as np
import matplotlib.pyplot as plt
from tkinter import filedialog
import os
from mpl_toolkits.mplot3d import Axes3D

global scan_dir
scan_dir = filedialog.askdirectory()
print(scan_dir)
x=os.listdir(scan_dir)

img={}

print(x)

for i in range(0,len(x)):

    print(i)

    img[i] = cv2.imread(x[i], cv2.IMREAD_GRAYSCALE)

    indices[i] = np.where(img[i]<100)

I get the following error...(None is the return of print(img[i] on 6th iteration of the loop)

None
Traceback (most recent call last):
File "C:\CodeRepository\US-3D\GettingCloser.py", line 55, in <module>
      indices[i] = np.where(img[i]<100)
TypeError: '<' not supported between instances of 'NoneType' and 'int'

I have the same problem if I try this

global scan_dir
scan_dir = filedialog.askdirectory()
print(scan_dir)
x=os.listdir(scan_dir)

img = cv2.imread(x[5], cv2.IMREAD_GRAYSCALE)

It will return that img is None. This is true for anything beyond the 5th image.

tbonts
  • 1
  • 1
  • What do you get for `print(x)`? – fractals Dec 04 '18 at 03:47
  • `['1.tif', '2.tif', '3.tif', '4.tif', '5.tif', '6.tif', '7.tif', '8.tif', '9.tif', '10.tif', '11.tif', '12.tif', '13.tif', '14.tif', '15.tif', '16.tif', '17.tif', '18.tif', '19.tif', '20.tif', '21.tif', '22.tif', '23.tif', '24.tif', '25.tif', '26.tif', '27.tif', '28.tif', '29.tif', '30.tif']` – tbonts Dec 04 '18 at 03:48
  • You mean `'5.tif'` is giving `None` value? If this is the case try reading that alone or remove that file from `list. We have to come to a conclusion if code or image has a problem. Then we move towards solving it. – Hayat Dec 04 '18 at 03:53
  • I have tried removing that image, as well as changing that image to have different coloration. Now, I just tried changing the file names to _01.tif_, _02.tif_, _03.tif_, _04.tif_, _05.tif_. I also tried changing _5.tif_ to _6.tif_. These both returned `None`. It seems that anything other that 1, 2, 3, 4, 5 as the file name returns `None` – tbonts Dec 04 '18 at 04:03
  • Check the question https://stackoverflow.com/questions/17949268/cv2-imread-always-returns-nonetype. –  Dec 04 '18 at 06:59

1 Answers1

0

Must be something wrong with the file. dicts are an unordered Data structure. Should not give error always on 5th iteration. However, I have made the changes which will not throw the error. But you need to debug that image

for i in range(0,len(x)):

    print(i)

    img[i] = cv2.imread(x[i], cv2.IMREAD_GRAYSCALE)
    if img[i]:

        indices[i] = np.where(img[i]<100)
Hayat
  • 1,539
  • 4
  • 18
  • 32
  • I have tried this and it returned `if img[i]: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()` – tbonts Dec 04 '18 at 03:27
  • This link might help. https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – Hayat Dec 04 '18 at 03:34
  • I have edited the question. I don't think I asked the right question. The problem seems to come before cv2.imread...? – tbonts Dec 04 '18 at 03:40