如何将图像垂直切割成两个相等大小的图像

问题描述 投票:5回答:1

所以我有一个800 x 600的图像,我想用OpenCV 3.1.0垂直切割成两张大小相同的图片。这意味着在剪切结束时,我应该有两个图像,每个图像为400 x 600,并存储在自己的PIL变量中。

这是一个例子:

Paper being cut into halves

谢谢。

编辑:我想要最有效的解决方案,所以如果该解决方案使用numpy拼接或类似的东西,那么去吧。

python opencv numpy image-resizing opencv3.1
1个回答
6
投票

您可以尝试以下代码,它将创建两个numpy.ndarray实例,您可以轻松地显示或写入新文件。

from scipy import misc

# Read the image
img = misc.imread("face.png")
height, width = img.shape

# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff]
s2 = img[:, width_cutoff:]

# Save each half
misc.imsave("face1.png", s1)
misc.imsave("face2.png", s2)

face.png文件是一个示例,需要替换为您自己的图像文件。

© www.soinside.com 2019 - 2024. All rights reserved.