OpenCV - NoneType不可下标



我不明白为什么我的代码抛出'NoneType'对象是不可订阅的错误,当我运行这个:

y = []
for every_img in sorted(os.listdir(easyocr_test)):
img = cv2.imread(easyocr_test + "/" + every_img)
left_image = img[13:40, 570:606]
y.append(left_image)

我希望yeasyocr_test文件夹中附加所有图像的特定区域。为什么它不让我这么做?我困惑。

我试着重新创建一个VENV,但无济于事。

完全错误:

Traceback (most recent call last):
File "/Users/xxx/Desktop/python_projects/test10/test1.py", line 12, in <module>
left_image = img[13:40, 570:606]
TypeError: 'NoneType' object is not subscriptable

为了避免在不需要的文件上迭代,我建议使用glob模式和pathlib:

from pathlib import Path
folder = Path(easyocr_test)       # Path to the folder containing the imgs
img_paths = folder.glob('*.png')  # or e.g. '*.jpg', adjust accordingly
y = []
for img_path in sorted(img_paths):
img = cv2.imread(img_path)
left_image = img[13:40, 570:606]
y.append(left_image)

最新更新