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

Ubuntu编写的Python二维码识别脚本在Windows运行报错求助

问题分析与解决办法

错误原因

Windows系统会自动在图片文件夹生成Thumbs.db文件,这是系统用于缓存缩略图的数据库文件,并非标准图片格式。你的脚本遍历Images_folder时,误将该文件当作图片读取,而处理图片的依赖库(如Pillow)无法识别其格式,因此抛出ValueError提示找不到合适的后端打开文件。Linux系统不会生成这类文件,所以之前运行无异常。

解决办法

1. 过滤指定格式的图片文件

遍历文件夹时,仅处理后缀为图片格式的文件,避免读取系统文件:

import os

# 定义支持的图片后缀
IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.bmp', '.gif')
images_dir = "Images_folder"

for filename in os.listdir(images_dir):
    # 匹配后缀(忽略大小写)
    if filename.lower().endswith(IMAGE_EXTENSIONS):
        file_path = os.path.join(images_dir, filename)
        # 执行你的图片识别、重命名逻辑
        process_image(file_path)

2. 直接排除系统特定文件

在遍历逻辑中跳过Thumbs.db(可同时添加desktop.ini等其他系统文件):

for filename in os.listdir(images_dir):
    # 跳过系统文件
    if filename in ('Thumbs.db', 'desktop.ini'):
        continue
    file_path = os.path.join(images_dir, filename)
    # 执行你的图片处理逻辑
    process_image(file_path)

3. 跨平台路径优化(可选)

使用pathlib模块处理路径,提升跨平台兼容性,避免Windows/Linux路径分隔符差异问题:

from pathlib import Path

images_dir = Path("Images_folder")
for img_path in images_dir.iterdir():
    # 跳过系统文件和子文件夹
    if img_path.name in ('Thumbs.db', 'desktop.ini') or img_path.is_dir():
        continue
    # 过滤图片格式
    if img_path.suffix.lower() in ('.jpg', '.jpeg', '.png'):
        # 执行你的图片处理逻辑
        process_image(img_path)

内容的提问来源于stack exchange,提问作者Droidux

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 01:03:12