Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以numpy数组格式分割图片?_Python_Arrays_Image_Numpy - Fatal编程技术网

Python 如何以numpy数组格式分割图片?

Python 如何以numpy数组格式分割图片?,python,arrays,image,numpy,Python,Arrays,Image,Numpy,我有一个形状的图像(3127825794,3)。我想知道如何使用np函数获取图片的MxN段。例如,从: 我想获得: 在numpy中,您可以像分割阵列一样分割图片 以下是您的图像示例: import numpy as np import matplotlib.pyplot as plt from PIL import Image img = np.array(Image.open("cat.jpg")) plt.imshow(img) 请记住,此处的拆分是原始数组中的

我有一个形状的图像(3127825794,3)。我想知道如何使用np函数获取图片的MxN段。例如,从:

我想获得:


在numpy中,您可以像分割阵列一样分割图片

以下是您的图像示例:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img = np.array(Image.open("cat.jpg"))
plt.imshow(img)


请记住,此处的拆分是原始数组中的视图,而不是包含新数据的数组,因此对视图所做的更改将更改原始数据。如果您不想这样做,可以在对阵列进行切片后复制数据。

这是否回答了您的问题?使
M
N
都等于2肯定不是澄清你想要什么的最好方法!
xs = img.shape[0]//2  # division lines for the picture
ys = img.shape[1]//2
# now slice up the image (in a shape that works well with subplots)
splits = [[img[0:xs, 0:ys], img[0:xs, ys:]], [img[xs:, 0:ys], img[xs:, ys:]]]

fig, axs = plt.subplots(2, 2)
for i in range(2):
    for j in range(2):
        axs[i][j].imshow(splits[i][j])