使用numpy处理图片——图片切割

大纲

  • 上下切分
  • 左右切分

在《使用numpy处理图片——滤镜》和《用numpy处理图片——模糊处理》中,我们认识到对三维数组使用dsplit方法按第3维度(深度)方向切分的方法。
使用numpy处理图片——图片切割_第1张图片
本文我们将介绍如何进行第一和第二维度切分,来达到图片切割的效果。

上下切分

上下切分也是按第一维度切分,使用的是vsplit方法。

import numpy as np
from PIL import Image

img = Image.open('the_starry_night.jpg')
data = np.array(img)

topBottom = np.vsplit(data, 2)
top = topBottom[0]
bottom = topBottom[1]

上面我们给vsplit第二个参数传递的是2,即将数组按第一维度切分为上下2部分。

左右切分

我们分别对之前切分的上下两部分,进行第二维度切分,使用的是hsplit方法。我们给hsplit第二个参数传递的是2,也就是说我们要将其切分成左右两部分。

leftRight = np.hsplit(top, 2)
topLeft = leftRight[0]
topRight = leftRight[1]

leftRight = np.hsplit(bottom, 2)
bottomLeft = leftRight[0]
bottomRight = leftRight[1]

于是构成上左、上右、下左和下右四部分。
使用numpy处理图片——图片切割_第2张图片

以梵高的《星空》为例。
使用numpy处理图片——图片切割_第3张图片
使用numpy处理图片——图片切割_第4张图片
使用numpy处理图片——图片切割_第5张图片
使用numpy处理图片——图片切割_第6张图片
使用numpy处理图片——图片切割_第7张图片

你可能感兴趣的:(numpy,numpy)