python把一张图片均分4份

def cut_out_image():
    path_img = r'./hh' #文件夹名称
    img_dir = os.listdir(path_img)
    for i in img_dir:
        # 根据图片名称提取id,方便重命名
        id = (i.split('.')[0])
        img = Image.open(os.path.join(path_img, i))
        size_img = img.size
        print(size_img)
        weight = int(size_img[0] // 2)
        height = int(size_img[1] // 2)
        flag = 0
        for j in range(2):
            for k in range(2):
                box = (weight * k, height * j, weight * (k + 1), height * (j + 1))
                region = img.crop(box)
                # 输出路径
                flag = flag + 1
                new_path = os.path.join("灰岩_cut_out", id + "_" + str(flag) + ".png")
                region.save(new_path)

python把一张图片均分4份_第1张图片
python把一张图片均分4份_第2张图片

你可能感兴趣的:(python,java,开发语言)