You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python无法找到指定路径:Jupyter Notebook图片读取报WinError3求助

图片路径读取错误修复

错误根源

  • image_path指向的是单个图片文件,但os.listdir()要求传入文件夹路径,系统找不到对应的文件夹,因此抛出WinError 3。
  • 即便路径修正,os.listdir()返回的是文件名列表,直接用os.path.join(image_path, image_files)会因类型不匹配报错(字符串与列表无法拼接)。

方案1:读取单张指定图片(01.jpg)

直接使用图片路径读取,无需os.listdir():

import cv2

# 单个图片的完整路径
image_path = r'C:\Users\ays\Desktop\IR\01.jpg'
img = cv2.imread(image_path)

# 先验证图片是否读取成功
if img is not None:
    cv2.imshow('image', img)
    cv2.waitKey(0)  # 必须添加,防止窗口一闪而过
    cv2.destroyAllWindows()  # 关闭所有OpenCV窗口
else:
    print("图片读取失败,请检查路径是否正确")

方案2:遍历读取文件夹内所有图片

如果需要处理IR文件夹下的所有图片,将路径改为文件夹路径:

import os
import cv2

# 文件夹路径(不是单个图片路径)
image_folder = r'C:\Users\ays\Desktop\IR'

# 遍历文件夹内的所有文件
for filename in os.listdir(image_folder):
    # 仅处理常见图片格式,避免读取非图片文件
    if filename.lower().endswith(('.jpg', '.png', '.jpeg')):
        img_path = os.path.join(image_folder, filename)
        img = cv2.imread(img_path)
        
        if img is not None:
            cv2.imshow(f'Image: {filename}', img)
            cv2.waitKey(0)
            cv2.destroyWindow(f'Image: {filename}')
        else:
            print(f"无法读取图片:{img_path}")

内容的提问来源于stack exchange,提问作者Ayşe Nur Eliçora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:10:48