使用python对图片resize
from PIL import Image
def resize_image(input_path, output_path, target_size):
# 打开图像
with Image.open(input_path) as img:
# 获取原始宽度和高度
original_width, original_height = img.size
# 计算缩放比例
aspect_ratio = int(original_width / original_height)/8*8
# 根据目标宽度或高度计算新的尺寸
if original_width > original_height:
new_width = target_size
new_height = int(target_size / aspect_ratio)
else:
new_height = target_size
new_width = int(target_size * aspect_ratio)
# 调整图像大小
resized_img = img.resize((new_width, new_height), Image.LANCZOS)
# 保存调整后的图像
resized_img.save(output_path)
# 示例用法
resize_image('D:/Profile/Desktop/gpt/1101_lte.png', 'D:/Profile/Desktop/gpt/resize_1101_lte.png', 16) # 将图像调整为宽度或高度为800