批量JPEG转JPG

from PIL import Image
import os

def jpeg2jpg(path_in, path_out):
    img = Image.open(path_in)
    img = img.convert("RGB")
    img.save(path_out, "JPEG", quality=80, optimize=True, progressive=True)

input_dir = "/root/autodl-tmp/caoyaodatas/val/"  # 输入目录
output_dir = "/root/autodl-tmp/val/"  # 输出目录

# 遍历输入目录下的所有子目录
for subdir in os.listdir(input_dir):
    subdir_path = os.path.join(input_dir, subdir)
    if os.path.isdir(subdir_path):
        output_subdir_path = os.path.join(output_dir, subdir)
        os.makedirs(output_subdir_path, exist_ok=True)
        for filename in os.listdir(subdir_path):
            if filename.endswith(".jpg"):
                try:
                    source = os.path.join(subdir_path, filename)
                    target = os.path.join(output_subdir_path, filename)
                    jpeg2jpg(source, target)
                except:
                    print(filename)

你可能感兴趣的:(python脚本,python,前端,服务器)