from PIL import Image
import os
# 定义原文件夹路径和目标文件夹路径
source_folder = "path/to/source/folder"
target_folder = "path/to/target/folder"
# 遍历原文件夹中的所有图片文件
for filename in os.listdir(source_folder):
if filename.endswith(".png"):
# 打开图片文件并转换格式
with Image.open(os.path.join(source_folder, filename)) as img:
img = img.convert('RGB')
img.save(os.path.join(target_folder, filename.replace('.png', '.jpg')))
.png转.jpg尺寸不变
from PIL import Image
import os
# 定义原文件夹路径和目标文件夹路径
source_folder = "path/to/source/folder"
target_folder = "path/to/target/folder"
# 遍历原文件夹中的所有图片文件
for filename in os.listdir(source_folder):
if filename.endswith(".jpeg"):
# 打开图片文件并转换格式
with Image.open(os.path.join(source_folder, filename)) as img:
img = img.convert('RGB')
img.save(os.path.join(target_folder, filename.replace('.jpeg', '.jpg')))
.jpeg转.jpg尺寸不变
将文件夹里的图片修改为指定尺寸
from PIL import Image
import os
# 定义原文件夹路径和目标文件夹路径
source_folder = "/home/uto/数据/img_dir"
target_folder = "/home/uto/数据/image_6"
# 遍历原文件夹中的所有图片文件
for filename in os.listdir(source_folder):
if filename.endswith(".jpg") or filename.endswith(".jpeg") or filename.endswith(".png"):
# 打开图片文件并调整大小
with Image.open(os.path.join(source_folder, filename)) as img:
img = img.resize((1280, 720))
# 保存到目标文件夹中并保持原有文件名
img.save(os.path.join(target_folder, filename))