【问题标题】:How to split a picture in numpy array format? [closed]如何以numpy数组格式分割图片? [关闭]
【发布时间】:2021-03-02 06:17:17
【问题描述】:

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

我想获得:

【问题讨论】:

标签: python arrays image numpy


【解决方案1】:

在 numpy 中,您可以像分割数组一样分割图片。

以下是您的图片示例:

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

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

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])

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-09
    • 2018-07-20
    • 2020-10-13
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    相关资源
    最近更新 更多