【问题标题】:Error permission 13 when convert an image转换图像时出现错误权限 13
【发布时间】:2021-03-04 07:52:44
【问题描述】:

我尝试使用 python 脚本在 webp 中转换我的目录中的所有图像,但是当我尝试使用函数时出现此错误:权限错误 13

from PIL import Image
import PIL
import os
import glob

files = os.listdir()

images = [file for file in files if file.endswith(('jpg', 'png', 'jfif', 'jpeg'))]

print(f"Images {images}")

images = [file for file in files if file.endswith(('jpg', 'png', 'jfif'))]

class Error(Exception):
    """Base class for other exceptions"""
    pass

def convert_image(image_path, image_type):

    im = Image.open(image_path)
    im = im.convert('RGB')
    image_name = image_path.split('.')[0]
    print(f"This is the image name: {image_name}")

    if image_type == 'jpg' or image_type == 'png' or image_type == 'jfif':
        im.save(f"{image_name}.webp", 'webp')
    else:
        raise Error

    for image in images:
        if image.endswith('jpg'):
            convert_image(image, image_type='jpg')
        elif image.endswith('png'):
            convert_image(image, image_type='png')
        elif image.endswith('jfif'):
            convert_image(image, image_type='jfif')    
        else:
            raise Error

convert_image('C:\\Users\\Dani\\Desktop\\Webp', 'png')        

这是我的脚本和目录

这是错误 我在运行脚本时得到的 我怎样才能摆脱这些权限或者我应该怎么做? enter image description here


    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\Dani\Desktop\Webp\converter.py", line 49, in <module>
  File "c:\Users\Dani\Desktop\Webp\converter.py", line 26, in convert_image
    image_name = image_path.split('.')[0]
  File "C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 2904, in open
    fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Dani\\Desktop\\Webp'

【问题讨论】:

  • 请显示完整的回溯。
  • 在您的问题中显示完整的堆栈跟踪和错误消息,而不仅仅是消息的编辑版本。
  • 完成@Barmar 我把所有错误都放了
  • “Permission denied”应该是一个足够清晰的错误信息。
  • Webp 是一个目录,您正尝试使用Image.open(image_path) 打开它。你希望它做什么?

标签: python python-imaging-library


【解决方案1】:

测试文件名是文件还是目录。如果是目录,则遍历目录中的文件并递归调用该函数。如果是文件,请转换它。

def convert_image(image_path, image_type):

    if image_type not in ('jpg', 'png', 'jfif'):
        raise Error

    if os.path.isdir(image_path):
        for file in glob.glob(os.path.join(image_path, '*.'+image_type)):
            convert_image(file, image_type)
        return
    
    im = Image.open(image_path)
    im = im.convert('RGB')
    image_name = image_path.split('.')[0]
    print(f"This is the image name: {image_name}")

    im.save(f"{image_name}.webp", 'webp')

【讨论】:

    猜你喜欢
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多