Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-在路径中将文件名作为变量时不读取图像_Python_Imread - Fatal编程技术网

Python-在路径中将文件名作为变量时不读取图像

Python-在路径中将文件名作为变量时不读取图像,python,imread,Python,Imread,我正在尝试使用imread读取Python2.7中的图像。下面是我的代码 with open('../Caltech Data 200-2011/CUB_200_2011/images.txt') as lines: for ln in islice(lines,60): j=0 for x in ln.split(): if j==1: print x, #Belo

我正在尝试使用
imread
读取Python2.7中的图像。下面是我的代码

with open('../Caltech Data 200-2011/CUB_200_2011/images.txt') as lines:
    for ln in islice(lines,60):
        j=0
        for x in ln.split():
            if j==1:
                print x,

                #Below is the line that is causing trouble
                img = cv2.imread('../Caltech Data 200-2011/CUB_200_2011/images/"%s"' % (x))

                cv2.imshow('Image',img)
                cv2.waitKey()
            j=j+1
上面的代码不起作用。当我打印变量
x
时,它打印正确。当我使用任意实际文件名而不是
imread
中的变量时,它读取并显示图像。但在使用变量时,它不会读取图像,并显示以下错误

error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\highgui\src\window.cpp:271: error: (-215) size.width>0 && size.height>0 in function cv::imshow
  • 我做错了什么
  • 是否有其他方法将变量包含在作为
    imread
    输入的路径中

  • 在阅读了

    之后,我尝试了上述代码。您的引号似乎太多了。试试这个:

    img = cv2.imread('../Caltech Data 200-2011/CUB_200_2011/images/%s' % (x))
    

    你的问题有错吗?你的意思是,“它不是在阅读图像并显示它”?@Robᵩ 对不起,我刚才不太清楚。我现在已经编辑过了。