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

TIFF转JPG处理忽略隐藏文件目录,代码报错致Qwiklabs验证失败

问题排查与修复方案

核心错误分析

你遇到的IsADirectoryError是因为代码遍历images/目录时,把其中的opt子目录当成图片文件尝试打开,直接触发报错。同时代码里的变量名大小写错误、输出文件名后缀未修正,这些都是导致Qwiklabs判定任务失败的关键原因。

具体修复点

  1. 修正变量名大小写问题
    Python是大小写敏感语言,代码里定义的InputPath和调用时的inputPath不一致,会抛出NameError,统一修正为:
inputPath = os.path.join(inPath, imagePath)
img = Image.open(inputPath)
  1. 遍历过程跳过目录
    os.listdir()会返回目录下所有条目(包括子目录),需要增加判断跳过目录,只处理文件:
for imagePath in os.listdir(inPath):
    inputPath = os.path.join(inPath, imagePath)
    # 跳过子目录,仅处理文件
    if not os.path.isfile(inputPath):
        continue
    # 后续图片处理逻辑
  1. 修正输出文件名后缀
    原代码直接沿用TIFF文件的后缀保存,会生成xxx.tiff格式的文件,不符合JPG要求,需要替换后缀:
# 提取文件名并替换后缀为.jpg
filename = os.path.splitext(imagePath)[0] + ".jpg"
fullOutPath = os.path.join(outPath, filename)
  1. 确保输出目录存在
    如果images/opt/icons/目录未提前创建,保存图片时会报错,需提前生成目录:
# 不存在则创建输出目录,已存在则不报错
os.makedirs(outPath, exist_ok=True)

修复后的完整代码

#!/usr/bin/env python3
from PIL import Image
import os

def main():
    # 原始图片存放目录
    inPath = "/home/student-04-feb1d1e1a7a1/images/"
    # 处理后图片输出目录
    outPath = "/home/student-04-feb1d1e1a7a1/images/opt/icons/"

    # 确保输出目录存在
    os.makedirs(outPath, exist_ok=True)

    for imagePath in os.listdir(inPath):
        inputPath = os.path.join(inPath, imagePath)
        # 跳过子目录,仅处理文件
        if not os.path.isfile(inputPath):
            continue
        
        try:
            img = Image.open(inputPath)
            # 将非RGB模式图片转换为RGB(TIFF可能为RGBA等模式)
            if img.mode != "RGB":
                img = img.convert("RGB")
            # 执行旋转、尺寸调整并保存为JPG
            filename = os.path.splitext(imagePath)[0] + ".jpg"
            fullOutPath = os.path.join(outPath, filename)
            img.rotate(90).resize((128, 128)).save(fullOutPath, "JPEG", optimize=True, quality=80)
        except Exception as e:
            print(f"处理文件{inputPath}失败: {e}")

if __name__ == '__main__':
    main()

Qwiklabs判定额外注意事项

  • 确认所有输出JPG文件尺寸严格为128x128,旋转方向符合任务要求
  • 输出目录路径images/opt/icons/需完全匹配,不能有拼写错误
  • 确保所有原始TIFF文件都被成功处理,无遗漏

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 07:57:51