from PIL import Image def crop_image_into_nine_parts(image_path): # 打开图片 image = Image.open(image_path) # 获取图片的宽度和高度 width, height = image.size # 计算裁剪后小图片的宽度和高度 part_width = width // 3 part_height = height // 3 parts = [] # 循环裁剪九等分的小图片 for i in range(3): for j in range(3): left = j * part_width top = i * part_height right = (j + 1) * part_width bottom = (i + 1) * part_height # 裁剪小图片 part = image.crop((left, top, right, bottom)) parts.append(part) return parts # 图片路径 image_path = "scripts/input/images/725.jpg" # 调用函数裁剪图片 nine_parts = crop_image_into_nine_parts(image_path) # 保存裁剪后的小图片 for idx, part in enumerate(nine_parts): part.save(f"part_{idx + 1}.jpg")