def jiasaw_transform(image, count=3): width, height = image.size item_width = int(width / count) item_height = int(height / count) patch_list = [] for j in range(0, count): for i in range(0, count): box = (i * item_width, j * item_height, (i + 1) * item_width, (j + 1) * item_height) patch = image.crop(box) patch_list.append(patch) return patch_list
输入:1. PIL图像 2. 等分的数量count
输出:等分切块后的PIL图像数组
def cancat_image(patch_list, shuffle=True): if shuffle: random.shuffle(patch_list) np_matrix = np.array([np.array(patch) for patch in patch_list]) count = int(math.sqrt(len(np_matrix))) img_list = [] for i in range(count): img = np.concatenate(np_matrix[i*count:(i+1)*count],1) img_list.append(img) img = np.vstack(img_list) return img
输入:1. 等分切块后的PIL图像数组 2. 是否打乱图像块顺序
输出:拼接后的ndarry矩阵
if __name__ == '__main__': img = Image.open('./linboli.jpg') list = jiasaw_transform(img) result = cancat_image(list) plt.imshow(result) plt.show()输入 输出 3等分情况 9等分
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)