简体   繁体   English

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

[英]Error permission 13 when convert an image

I try to convert all images from my directory in webp using a python script but when I try to use function I get this error: Permission error 13我尝试使用 python 脚本在 webp 中转换我的目录中的所有图像,但是当我尝试使用 function 时出现此错误:权限错误 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')        

This is my script and directory这是我的脚本和目录

And this is the errors Which I get when I run the script How can I get rid of these permissions or what should I do?这是我在运行脚本时遇到的错误我怎样才能摆脱这些权限或者我应该怎么做? 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'

Test whether the filename is a file or a directory.测试文件名是文件还是目录。 If it's a directory, loop through the files in the directory and call the function recursively.如果是目录,遍历目录下的文件,递归调用function。 If it's a file, convert it.如果是文件,请转换它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM