imgUtils.py
806 Bytes
from PIL import Image
# 加载原始图像
image_path = "./train/threecode/file_1704852415_6623.jpg"
original_image = Image.open(image_path)
# 放大图片
scale_factor = 2 # 放大倍数
scaled_image = original_image.resize(
(original_image.width * scale_factor, original_image.height * scale_factor)
)
# 裁剪放大后的图片
crop_left = scaled_image.width // 2 - original_image.width // 2 # 裁剪左上角 x 坐标
crop_top = scaled_image.height // 2 - original_image.height // 2 # 裁剪左上角 y 坐标
crop_width = original_image.width # 裁剪宽度
crop_height = original_image.height # 裁剪高度
cropped_image = scaled_image.crop(
(crop_left, crop_top, crop_left + crop_width, crop_top + crop_height)
)
# 保存裁剪后的图片
cropped_image.save("cropped_image.jpg")