2017-02-19 120 views
0

我想通過目錄中的不同圖像文件。我使用Jupyter來運行我的Python代碼。但是我不斷收到這個錯誤。以下是我的代碼和我收到的錯誤。在目錄中讀取JPG文件?

CODE:

import os 
import os.path 
for img in os.listdir('test_images'): 
    if img.endswith("jpg"): 
     scriptpath = os.path.dirname(img) 
     print(os.path.join('test_images', img)) 
     # Read in the image 
     image = os.path.join(scriptpath, img) 
     image = mpimg.imread(image) 
     # Grab the x and y size and make a copy of the image 
     ysize = image.shape[0] 
     xsize = image.shape[1] 
     color_select = np.copy(image) 
     # Define color selection criteria 
     red_threshold = 200 
     green_threshold = 200 
     blue_threshold = 200 

     rgb_threshold = [red_threshold, green_threshold, blue_threshold] 

     # Do a boolean or with the "|" character to identify 
     # pixels below the thresholds 
     thresholds = (image[:,:,0] < rgb_threshold[0]) \ 
        | (image[:,:,1] < rgb_threshold[1]) \ 
        | (image[:,:,2] < rgb_threshold[2]) 
     color_select[thresholds] = [red_threshold,green_threshold,blue_threshold] 
     plt.imshow(color_select) 
     # Display the image     
     plt.imshow(color_select) 
     continue 
    else: 
     continue 

OUTPUT:

test_images/solidWhiteCurve.jpg 

錯誤:

FileNotFoundError       Traceback (most recent call last) 
<ipython-input-3-6edf7c0860b7> in <module>() 
     7   # Read in the image 
     8   image = os.path.join(scriptpath, img) 
----> 9   image = mpimg.imread(image) 
    10   # Grab the x and y size and make a copy of the image 
    11   ysize = image.shape[0] 

/Users/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py in imread(fname, format) 
    1225 
    1226  if ext not in handlers: 
-> 1227   im = pilread(fname) 
    1228   if im is None: 
    1229    raise ValueError('Only know how to handle extensions: %s; ' 

/Users/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/matplotlib/image.py in pilread(fname) 
    1203   except ImportError: 
    1204    return None 
-> 1205   with Image.open(fname) as image: 
    1206    return pil_to_array(image) 
    1207 

/Users/steveburgos/anaconda/envs/carnd-term1/lib/python3.5/site-packages/PIL/Image.py in open(fp, mode) 
    2310 
    2311  if filename: 
-> 2312   fp = builtins.open(filename, "rb") 
    2313 
    2314  try: 

FileNotFoundError: [Errno 2] No such file or directory: 'solidWhiteCurve.jpg' 
+1

'scriptpath'將始終爲空字符串,因爲'os.listdir'返回裸文件名。嘗試打印'os.path.join(scriptpath,img)'(您打開的路徑)而不是'os.path.join('test_images',img)'。 – unutbu

+0

@unutbu你真棒!這工作完美!非常感謝! –

+1

我建議使用['glob.glob'](https://docs.python.org/3/library/glob.html#glob.glob)並以'* .jpg'結尾的_pathname_參數。 – martineau

回答

2

你有一個路徑不匹配你的代碼,你的錯誤是清楚地顯示出它(文件不找到)。當你這樣做:

for img in os.listdir('test_images'): 

您列出當前目錄中的test_images目錄。該img將包含在file1.ext形式,file2.ext等爲os.listdir()僅列出它的文件名和目錄,所以當你打電話值:

scriptpath = os.path.dirname(img) 

你基本上是GES任何事情,因爲沒有按img」 t包含任何路徑信息。所以,最後,當你這樣做:

image = os.path.join(scriptpath, img) 

你在技術上僅傳遞文件名作爲scriptpath是空的。由於您的映像位於test_images子目錄中,而不在您的工作目錄中,因此您通常會找不到文件。

有多種方法可以解決這個問題,最簡單的是隻申報一個查找目錄中的變量,並在需要的時候使用它,例如:

target_path = "test_images" 
# ... 
for img in os.listdir(target_path): 
# ... 
    image = os.path.join(target_path, img) 
0

在我看來,我更喜歡使用glob庫來返回目錄中的文件列表。 import glob print (glob.glob("/home/peter/pictures/*.png")

回報: ['/home/peter/pictures/pic1.png', '/home/peter/pictures/pic2.png', '/home/peter/pictures/pic3.png', ...ect]

如果你想繼續你的方法,我相信你沒有給予正確的路徑的文件夾目錄。 想一想,該程序如何知道test_images所在的位置。