split image

input_path = "result_1213"
output_path = "split_result_1213"
if not os.path.exists(output_path):
    os.makedirs(output_path)

files_name = glob.glob(input_path+"/"+"*.png")
for name in files_name:
    img = Image.open(name)
    width = img.size[0]
    height = img.size[1]
    width_distance = width * 1.0 /3
    height_distance = height * 1.0 /2
    for w_idx in range(3):
        for h_idx in range(2):
            box = [w_idx * width_distance, h_idx * height_distance,(w_idx + 1) * width_distance,(h_idx + 1) *height_distance ]
            base_name = os.path.basename(name).split(".")[0]
            ext = os.path.basename(name).split(".")[-1]
            output_name = base_name + "_" + str(w_idx) + "_" + str(h_idx) + "." + ext
            img.crop(box).save(os.path.join(output_path, output_name))

你可能感兴趣的:(split image)