本文用途:webp,gif修改为jpg格式
本代码仅供教学使用,任何非法行为与本帖无关
超级规范的代码 —— 供大家免费使用、教学
from pathlib import Path
from PIL import Image
def convert_to_jpg(img_path):
"""
将指定路径的图片文件转换为JPEG格式
"""
# 读取原始图片
with Image.open(img_path) as img:
# 检查图片格式
if img.format not in ['WEBP', 'GIF']:
print(f"Unsupported image format: {img.format}")
return
# 转换为RGB模式
img = img.convert('RGB')
# 获取输出文件路径
out_path = img_path.with_suffix('.jpg')
# 保存为JPEG格式
img.save(out_path, quality=80)
print(f"Image converted: {out_path}")
if __name__ == '__main__':
# 指定待转换图片所在文件夹(请替换为你自己的文件夹路径)
dir_path = Path(r'C:\Users\lenovo\Desktop\555')
# 遍历文件夹中所有的文件和子文件夹
for file_path in dir_path.glob('**/*.*'):
# 如果文件是WebP或GIF格式,则进行转换
if file_path.suffix.upper() in ['.WEBP', '.GIF']:
convert_to_jpg(file_path)
使用效果图
还有什么问题可在评论区提问