第一部分:考点和作答区
考点:
- 图像处理库(如:PIL)的使用
- 图像裁剪
- 图像调整大小
- 图像保存
题目描述: 编写一个Python函数 process_image
,该函数接受一个图像文件路径作为输入,并返回一个新图像。新图像应包含原图像的左上角部分,并且大小调整为原始图像的一半。
例如,如果输入的图像文件路径为 path_to_image.jpg
,生成的图像文件路径应为 processed_image.jpg
。
作答区:
# 请在此处编写你的代码
答案
from PIL import Image
def process_image(input_path, output_path):
with Image.open(input_path) as img:
# 裁剪左上角部分
cropped_img = img.crop((0, 0, img.width // 2, img.height // 2))
# 调整大小
resized_img = cropped_img.resize((img.width // 2, img.height // 2), Image.ANTIALIAS)
# 保存新图像
resized_img.save(output_path)
# 测试代码
input_path = 'path_to_image.jpg'
output