PIL 图像转换 - TIFF 到 JPG 并在处理过程中忽略隐藏文件和目录

问题描述 投票:0回答:2

我已经编写了代码,程序将 jpeg 输出到正确的目录 images/opt/icons。我还能够运行 Python 3 并打开并检查文件类型和大小特征。所以我相信它工作正常但是,Qwiklabs 不认为它是成功的,我无法继续。我已经尝试过这个练习超过5次了!尽管代码成功转换图像并将它们放置在正确的目录中,但它确实会引发错误。 回溯(最近一次调用最后一次): 文件“/home/student-04-feb1d1e1a7a1/./convert.py”,第 33 行,位于 主要的() 文件“/home/student-04-feb1d1e1a7a1/./convert.py”,第 20 行,在 main 中 img = Image.open(输入路径) 文件“/home/student-04-feb1d1e1a7a1/.local/lib/python3.9/site-packages/PIL/Image.py”,第 3218 行,打开 fp =builtins.open(文件名, "rb") IsADirectoryError: [Errno 21] 是一个目录: '/home/student-04-feb1d1e1a7a1/images/opt'

这是我的代码:-它是 Pillow Tutorial 和 Geek for Geeks 代码的混合体。

    #!/usr/bin/env python3

    # Code to apply operations on all the images
    # present in a folder one by one
    # operations such as rotating, cropping,
    from PIL import Image
    from PIL import ImageFilter
    import os

    def main():
         # path of the folder containing the raw images
        inPath ="/home/student-04-feb1d1e1a7a1/images/"

        # path of the folder that will contain the modified image
        outPath ="/home/student-04-feb1d1e1a7a1/images/opt/icons/"

        for imagePath in os.listdir(inPath):
            # imagePath contains name of the image
            InputPath = os.path.join(inPath, imagePath)
            img = Image.open(inputPath)
            # inputPath contains the full directory name
            if img.mode != "RGB":
              img = img.convert("RGB")

            fullOutPath = os.path.join(outPath,imagePath)
           # fullOutPath contains the path of the output
            # image that needs to be generated
            img.rotate(90).resize((128,128)).save(fullOutPath, "JPEG", optimize=True, quality=80)
            #print(fullOutPath)

    # Driver Function
    if __name__ == '__main__':
        main()
operating-system python-imaging-library jpeg tiff
2个回答
0
投票

您在 for 循环中有一个名为

InputPath
且带有大写“I”的变量,但在后续代码中使用带有小写“i”的
inputPath


0
投票

您的程序正在尝试打开自身,假设它是一个图像。您的

os.listdir()
返回所有内容 - 不仅仅是图像。

也许尝试使用

glob
模块:

from glob import glob

for img in glob("*.png"):
    ...
    ...

顺便说一下,您可能还想要:

im.rotate(90, expand=True)
© www.soinside.com 2019 - 2024. All rights reserved.