# -*- coding: utf-8 -*- # @File : 降低图片分辨率.py # @Author : len # @Time : 2024/1/12 10:17 import os from PIL import Image def resize_image(image_path, output_path, new_width, new_height): # 调整图片大小并保存 with Image.open(image_path) as img: resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) resized_img.save(output_path) # 遍历文件夹并调整所有图片的大小 folder_path = r'D:\Waste\嵌入式\模块一\任务三\img\原图片' output_folder = r'D:\Waste\嵌入式\模块一\任务三\img\调整后' new_width = 300 new_height = 150 # 确保输出文件夹存在 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历文件夹内的所有文件 for filename in os.listdir(folder_path): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')): image_path = os.path.join(folder_path, filename) output_path = os.path.join(output_folder, filename) # 调用函数以调整图片大小 resize_image(image_path, output_path, new_width, new_height)