Я пытаюсь пройти через различные файлы изображений в каталоге. Я использую Jupyter для запуска моего кода на Python. Однако я продолжаю получать эту ошибку. Ниже мой код и ошибка, которую я получаю.Прочитать JPG-файлы в каталоге?
КОД:
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
ВЫВОД:
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'
'scriptpath' всегда будет пустой строкой, потому что' os.listdir' возвращает искомые имена файлов. Попробуйте напечатать 'os.path.join (scriptpath, img)' (путь, который вы открываете) вместо 'os.path.join ('test_images', img)'. – unutbu
@unutbu Ты потрясающий! Это сработало отлично! Спасибо огромное! –
Я бы предложил использовать ['glob.glob'] (https://docs.python.org/3/library/glob.html#glob.glob) с аргументом _pathname_, заканчивающимся на' * .jpg'. – martineau