Python 如何以numpy数组格式分割图片?
我有一个形状的图像(3127825794,3)。我想知道如何使用np函数获取图片的MxN段。例如,从: 我想获得: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) 请记住,此处的拆分是原始数组中的
在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])