#图片分割函数
import cv2
import matplotlib.pyplot as plt
img=cv2.imread("test.png")
#转换成RGB格式
img=img[:,:,::-1]
#arr接受原图像 shape接受一个元组或者列表 如分割成2x3
def parte(arr,shape):
#算出横向和纵向步长
width=int(arr.shape[1]/shape[1])
height=int(arr.shape[0]/shape[0])
print(height,width)
#分图列表
arrs=[]
for i in range(shape[0]):
for j in range(shape[1]):
#制作分图
temp_array=arr[i*height:(i+1)*height,j*width:(j+1)*width]
arrs.append(temp_array)
plt.figure(dpi=160)
for i in range(shape[0]*shape[1]):
plt.subplot(shape[0],shape[1],i+1)
plt.imshow(arrs[i])
plt.show()
#调用函数
arr_part=parte(img,[2,3])
查看效果