电车
精华
|
战斗力 鹅
|
回帖 0
注册时间 2024-2-29
|
- import os
- import subprocess
- import shutil
- # 设置目标文件夹和7-Zip路径
- target_dir = r"这里填写你打算处理漫画的文件夹地址"
- seven_zip_path = r"这里填写7-zip的exe文件位置"
- # 图片转换函数
- def convert_images(directory):
- for root, _, files in os.walk(directory):
- for file in files:
- if file.endswith(".webp"):
- webp_path = os.path.join(root, file)
- jpg_path = os.path.join(root, file.replace(".webp", ".jpg"))
- command = [
- "nconvert",
- "-out", "jpeg",
- "-q", "85",
- "-o", jpg_path,
- "-D",
- webp_path
- ]
- result = subprocess.run(command, capture_output=True, text=True)
- if result.returncode != 0:
- print(f"Error converting {webp_path}: {result.stderr}")
- else:
- print(f"Converted and removed {webp_path}")
- # 压缩并删除文件夹函数
- def zip_and_delete_folders(directory):
- for root, dirs, _ in os.walk(directory):
- for folder in dirs:
- folder_path = os.path.join(root, folder)
- zip_path = f"{folder_path}.zip"
- command = [seven_zip_path, "a", zip_path, folder_path]
- result = subprocess.run(command, capture_output=True, text=True)
-
- # 检查压缩是否成功
- if result.returncode != 0:
- print(f"Error compressing {folder_path}: {result.stderr}")
- else:
- print(f"Compressed {folder_path} to {zip_path}")
-
- # 压缩成功后删除原文件夹
- try:
- shutil.rmtree(folder_path)
- print(f"Deleted folder {folder_path}")
- except Exception as e:
- print(f"Error deleting folder {folder_path}: {e}")
- # 执行转换与压缩
- convert_images(target_dir)
- zip_and_delete_folders(target_dir)
复制代码
需要python,还要把nconvert放入环境变量 |
|