这个代码将会读取指定文件夹中的所有.jpg
和.png
文件,并将它们的大小降低一半,然后将新的图片保存在同一文件夹下,文件名前缀为small_
。你可以根据需要修改代码中的文件夹路径、文件类型、调整大小的比例和新文件名前缀。
from PIL import Image
import os
# 读取文件夹中的所有图片
folder_path = "D:/A-Materials/论文材料"
for filename in os.listdir(folder_path):
if filename.endswith(".jpg") or filename.endswith(".png"):
# 打开图片并调整大小
image_path = os.path.join(folder_path, filename)
with Image.open(image_path) as img:
img = img.resize((img.width // 2, img.height // 2))
# 保存新的图片
new_filename = "small_4" + filename
new_image_path = os.path.join(folder_path, new_filename)
img.save(new_image_path)