|
from PIL import Image
import os
# 定义根目录
root_dir = "D:\\"
# 定义两个目标文件夹
left_dir = os.path.join(root_dir, "Left")
right_dir = os.path.join(root_dir, "Right")
# 创建目标文件夹,如果不存在
os.makedirs(left_dir, exist_ok=True)
os.makedirs(right_dir, exist_ok=True)
# 列出根目录下的所有 JPG 图片
jpg_files = [f for f in os.listdir(root_dir) if f.lower().endswith('.jpg')]
for file_name in jpg_files:
# 打开图片
img_path = os.path.join(root_dir, file_name)
img = Image.open(img_path)
# 获取图片尺寸
width, height = img.size
# 计算中间点
mid_point = width // 2
# 左半部分
left_img = img.crop((0, 0, mid_point, height))
left_img_path = os.path.join(left_dir, "left_" + file_name)
left_img.save(left_img_path)
# 右半部分
right_img = img.crop((mid_point, 0, width, height))
right_img_path = os.path.join(right_dir, "right_" + file_name)
right_img.save(right_img_path)
print(f"Processed {file_name} into {left_img_path} and {right_img_path}")
print("Images have been successfully divided and saved.")
|
|