可以使用Python中的Pillow库来完成这个任务。以下是一个简单的代码示例,它将一个文件夹中的所有256x256像素的图片缩小为128x128像素并保存到另一个文件夹中。
from PIL import Image
import os
input_folder = '/path/to/input/folder'
output_folder = '/path/to/output/folder'
new_size = (128, 128)
# Loop through all the files in the input folder
for filename in os.listdir(input_folder):
# Check if the file is an image
if filename.endswith('.png') or filename.endswith('.jpg'):
# Open the image and resize it
img = Image.open(os.path.join(input_folder, filename))
img = img.resize(new_size)
# Save the resized image to the output folder
new_filename = os.path.splitext(filename)[0] + '_resized.png'
img.save(os.path.join(output_folder, new_filename))
在这个例子中:
input_folder 是包含要缩小的图片的文件夹路径
output_folder 是将缩小的图片保存到的文件夹路径
new_size 是缩小后的图片尺寸
代码会检查每个文件是否是图片,如果是,就打开它、缩小它、并将其保存到输出文件夹中。