0

I have created a function which adds all my images to a list. The function is as follows:

def load_data(train_path,test_path):
    X_train=[]
    X_test=[]
    for i in os.listdir(train_path):
        X_train.append(i)
    for j in os.listdir(test_path):
        X_test.append(j)
        
    return X_train,X_test

When I try to display Image using indexing X_train[10] I get a file not found Error.

img=mpimg.imread(X_train[10])
imgplot = plt.imshow(img)
plt.show()

The error is as Followed:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-7-869e21232029> in <module>()
----> 1 img=mpimg.imread(X_train[10])
      2 imgplot = plt.imshow(img)
      3 plt.show()

/Users/ViditShah/anaconda/envs/dl/lib/python3.6/site-packages/matplotlib/image.py in imread(fname, format)
   1295             return handler(fd)
   1296         else:
-> 1297             with open(fname, 'rb') as fd:
   1298                 return handler(fd)
   1299     else:

FileNotFoundError: [Errno 2] No such file or directory: 'scan_0001001.png'

1 Answer 1

1

listdir() only returns the file name, not the full path. You need to store the full file path in your list

X_train.append(os.path.join(train_path, i))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.