Python 转换图像时出现错误权限13

Python 转换图像时出现错误权限13,python,python-imaging-library,Python,Python Imaging Library,我尝试使用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}&q

我尝试使用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')        
这是我的脚本和目录

这就是错误 当我运行脚本时得到的 我如何摆脱这些权限,或者我应该怎么做?


runpy.run\u路径(target\u as\u str,run\u name=compat.force\u str(“\uuuu main\uuu”))
文件“C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py”,第268行,在运行路径中
返回运行模块代码(代码、初始化全局、运行名称、,
文件“C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py”,第97行,在运行模块代码中
_运行代码(代码、mod_globals、init_globals、,
文件“C:\Users\Dani\AppData\Local\Programs\Python\Python39\lib\runpy.py”,第87行,在运行代码中
exec(代码、运行\全局)
文件“c:\Users\Dani\Desktop\Webp\converter.py”,第49行,在
文件“c:\Users\Dani\Desktop\Webp\converter.py”,第26行,在convert\U图像中
image_name=image_path.split('.')[0]
文件“C:\Users\Dani\AppData\Local\Programs\Python\39\lib\site packages\PIL\Image.py”,第2904行,处于打开状态
fp=内置的.open(文件名为“rb”)
PermissionError:[Errno 13]权限被拒绝:“C:\\Users\\Dani\\Desktop\\Webp”

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

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')

请显示完整的回溯。在您的问题中显示完整的堆栈跟踪和错误消息,而不仅仅是消息的编辑版本。Done@Barmar i put all errors“Permission denied”应该是一条足够清晰的错误消息。
Webp
是一个目录,您正试图使用
Image打开它。open(Image\u path)
。您希望它做什么?
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')