如何将一个图像垂直切割成两个大小相等的图像
How to Cut an Image Vertically into Two Equal Sized Images
所以我有一张 800 x 600 的图像,我想使用 OpenCV 3.1.0 将其垂直切割成两张同样大小的图片。这意味着在剪辑结束时,我应该有两个图像,每个图像为 400 x 600,并存储在它们自己的 PIL 变量中。
这是一个例子:
谢谢。
编辑:我想要最有效的解决方案,所以如果该解决方案使用 numpy 拼接或类似的东西,那就去做吧。
您可以尝试以下代码,这将创建两个 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
文件为示例,需要替换为您自己的图片文件。
您可以定义以下函数来简单地将您想要的每个图像切成两个垂直部分。
def imCrop(x):
height,width,depth = x.shape
return [x[height , :width//2] , x[height, width//2:]]
然后您可以简单地绘制图像的右侧部分:
plt.imshow(imCrop(yourimage)[1])
import cv2
# Read the image
img = cv2.imread('your file name')
print(img.shape)
height = img.shape[0]
width = img.shape[1]
# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff]
s2 = img[:, width_cutoff:]
cv2.imwrite("file path where to be saved", s1)
cv2.imwrite("file path where to be saved", s2)
所以我有一张 800 x 600 的图像,我想使用 OpenCV 3.1.0 将其垂直切割成两张同样大小的图片。这意味着在剪辑结束时,我应该有两个图像,每个图像为 400 x 600,并存储在它们自己的 PIL 变量中。
这是一个例子:
谢谢。
编辑:我想要最有效的解决方案,所以如果该解决方案使用 numpy 拼接或类似的东西,那就去做吧。
您可以尝试以下代码,这将创建两个 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
文件为示例,需要替换为您自己的图片文件。
您可以定义以下函数来简单地将您想要的每个图像切成两个垂直部分。
def imCrop(x):
height,width,depth = x.shape
return [x[height , :width//2] , x[height, width//2:]]
然后您可以简单地绘制图像的右侧部分:
plt.imshow(imCrop(yourimage)[1])
import cv2
# Read the image
img = cv2.imread('your file name')
print(img.shape)
height = img.shape[0]
width = img.shape[1]
# Cut the image in half
width_cutoff = width // 2
s1 = img[:, :width_cutoff]
s2 = img[:, width_cutoff:]
cv2.imwrite("file path where to be saved", s1)
cv2.imwrite("file path where to be saved", s2)