【问题标题】:Displaying Image in python在 python 中显示图像
【发布时间】:2018-06-22 08:28:55
【问题描述】:

我创建了一个函数,可以将我的所有图像添加到列表中。 函数如下:

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

当我尝试使用索引 X_train[10] 显示图像时,我得到一个文件未找到错误。

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

错误如下:

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'

【问题讨论】:

    标签: matplotlib python-3.5


    【解决方案1】:

    listdir() 只返回文件名,而不是完整路径。 您需要将完整的文件路径存储在列表中

    X_train.append(os.path.join(train_path, i))
    

    【讨论】:

      猜你喜欢
      • 2010-11-24
      • 2019-05-07
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 2012-07-18
      • 1970-01-01
      相关资源
      最近更新 更多