vidit02100
vidit02100

Reputation: 153

Displaying Image in python

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'

Upvotes: 0

Views: 351

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

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))

Upvotes: 1

Related Questions