Python opencv将一张图片任意N等分并保存

import cv2
src = cv2.imread('100_0124_0013_pre_disaster.JPG', -1)
cnt = 1
num = 1
sub_images = []
sub_image_num = 4
src_height, src_width = src.shape[0], src.shape[1]
sub_height = src_height // sub_image_num
sub_width = src_width // sub_image_num
for j in range(sub_image_num):
    for i in range(sub_image_num):
        if j < sub_image_num - 1 and i < sub_image_num - 1:
            image_roi = src[j * sub_height: (j + 1) * sub_height, i * sub_width: (i + 1) * sub_width, :]
        elif j < sub_image_num - 1:
            image_roi = src[j * sub_height: (j + 1) * sub_height, i * sub_width:, :]
        elif i < sub_image_num - 1:
            image_roi = src[j * sub_height:, i * sub_width: (i + 1) * sub_width, :]
        else:
            image_roi = src[j * sub_height:, i * sub_width:, :]
        sub_images.append(image_roi)
for i, img in enumerate(sub_images):
    cv2.imwrite('sub_img_' + str(i) + '.png', img)

 

 

你可能感兴趣的:(Python)