以下是Python代码实现:

import os
from PIL import Image

# 定义等份裁剪函数
def cut_image(image):
    width, height = image.size
    item_width = int(width / 2)
    item_height = int(height / 2)
    box_list = []
    for i in range(0, 2):
        for j in range(0, 2):
            box = (j * item_width, i * item_height, (j + 1) * item_width, (i + 1) * item_height)
            box_list.append(box)
    image_list = [image.crop(box) for box in box_list]
    return image_list

# 遍历文件夹里所有图片文件
folder_path = 'your_folder_path'
for filename in os.listdir(folder_path):
    if filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.jpeg'):
        # 打开原始图片
        filepath = os.path.join(folder_path, filename)
        image = Image.open(filepath)
        # 裁剪成四份
        image_list = cut_image(image)
        # 保存裁剪后的图片
        for i, img in enumerate(image_list):
            save_path = os.path.join(folder_path, '1/4', filename[:-4] + f'_{i+1}.jpg')
            img.save(save_path)

注意,这段代码使用了Pillow库(也称为PIL),需要先安装该库,可以使用以下命令进行安装:

pip install pillow

另外,需要把变量 folder_path 替换成实际的文件夹路径


原文地址: http://www.cveoy.top/t/topic/eHOs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录