针对图片中的指定区域进行裁剪,然后把裁剪区域的图像进行其他处理,计算完之后再把图片粘贴只原来区域合成新的结果图
from PIL import Image import model_test_plot from pathlib import Path def crop_image(image_path, box): # 打开原始图像 image = Image.open(image_path) # 定义剪切区域的坐标 # 剪切图像 cropped_image = image.crop(box) file = Path(image_path).name print('file==', file) if '.' in file: image_name = str(file).split('.')[0] print('image_name==', image_name) cropped_image.save(image_name + '_cropp.jpg') # 在原始图像上粘贴剪切图像 image.paste(cropped_image, box) # 保存结果图像 image.save(image_name + '_result2.jpg') if __name__ == '__main__': image_path = r'4K图片路径' box = (0, 0, 3840, 1080) # (left, upper, right, lower)上半部分图片 crop_image(image_path, box)