【问题标题】:from single jpg to web conversion to all files in dir从单个 jpg 到 web 转换到目录中的所有文件
【发布时间】:2023-01-29 23:16:36
【问题描述】:

我制作了 python 代码将 jpg 转换为 webp

from PIL import Image
import PIL
import os
import glob



# Open The Image:
image_path = "D:/..../image_name.jpg"

image = Image.open(image_path)


# Convert the image to RGB Colour:
image = image.convert('RGB')

# Save The Image as webp:
new_image_path = "D:/.../converted_image_name.webp"

image.save(new_image_path, 'webp')

#open converted image for inspection

new_image = Image.open(new_image_path)
new_image.show()

不知道如何构建一个函数来对文件夹中的所有文件进行转换,同时保留原始文件名。

【问题讨论】:

    标签: python image-size


    【解决方案1】:

    这是您的问题的有效解决方案:

    from pathlib import Path
    
    from PIL import Image
    import PIL
    import os
    import glob
    
    # the path to the directory containing the images
    directory_path = 'D:PATH'
    
    def convert_images(directory_path):
        for image_path in os.listdir(directory_path):
            if image_path.endswith(".jpg"):
                image = Image.open(os.path.join(directory_path, image_path))
                # get image filename without an extension
                name = Path(image_path).stem
                rgb_image = image.convert('RGB')
                new_image_path = f"{name}.webp"
                rgb_image.save(os.path.join(directory_path, new_image_path), 'webp')
                new_image = Image.open(new_image_path)
                new_image.show()
                continue
            else:
                continue
    
    
    convert_images(directory_path)
    

    【讨论】:

    • 我收到这个错误
    • 你遇到了什么错误?
    • FileNotFoundError: [Errno 2] No such file or directory: 'resized_marie_jo-lingerie-underwired_bra-avero-0100410-%28maincolor%29-0_3515169.jpg' 所以它可以找到文件名,但不会打开它
    • 这很奇怪。您是否将 directory_path = 'D:PATH' 变量设置为包含图像的目录(而不是实际图像路径)?另外,是否显示了错误行号?它可以帮助我了解哪里有问题。
    • 错误行 ----> 7 image = Image.open(image_path)。 directory_path = 'D:/Lokale schijf/VANDERLINDEN/datafiles/BRANDS/VDV/images/SS2023/Python_API_imports/'
    【解决方案2】:

    最终代码

    from pathlib import Path
    
    from PIL import Image
    import PIL
    import os
    import glob
    
    # the path to the directory containing the images
    directory_path =  'D:/Lokale schijf/SS2023/Python_API_imports/'
    
    
    def convert_images(directory_path):
        for image_path in os.listdir(directory_path):
            if image_path.endswith(".jpg"):
                image = Image.open(os.path.join(directory_path, image_path)) 
                # get image filename without an extension
                name = Path(image_path).stem
                rgb_image = image.convert('RGB')
                new_image_path = f"{name}.webp"
                rgb_image.save(os.path.join(directory_path, new_image_path), 'webp') 
                new_image = Image.open(new_image_path)
                new_image.show()
                continue
            
    
    convert_images(directory_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      相关资源
      最近更新 更多