Python 如何将图像垂直切割为两个大小相等的图像
所以我有一张800x600的图片,我想用OpenCV 3.1.0把它垂直切割成两张大小相等的图片。这意味着在剪切结束时,我应该有两个分别为400 x 600的图像,并存储在各自的PIL变量中 下面是一个例子: 多谢各位Python 如何将图像垂直切割为两个大小相等的图像,python,opencv,numpy,image-resizing,opencv3.1,Python,Opencv,Numpy,Image Resizing,Opencv3.1,所以我有一张800x600的图片,我想用OpenCV 3.1.0把它垂直切割成两张大小相等的图片。这意味着在剪切结束时,我应该有两个分别为400 x 600的图像,并存储在各自的PIL变量中 下面是一个例子: 多谢各位 编辑:我想要最有效的解决方案,因此如果该解决方案使用numpy拼接或类似的方法,那么就试试吧。您可以尝试以下代码,这些代码将创建两个实例,您可以轻松地显示或写入新文件 from scipy import misc # Read the image img = misc.imr
编辑:我想要最有效的解决方案,因此如果该解决方案使用numpy拼接或类似的方法,那么就试试吧。您可以尝试以下代码,这些代码将创建两个实例,您可以轻松地显示或写入新文件
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])
很好的图表!!!谢谢你的回答。我唯一去掉的是第三个变量/索引:
height,width,u=img.shape
,s1=img[:,:width\u cutoff,:]
和s2=img[:,width\u cutoff:,:]
因为图像是二维的,程序在我删除它们之前给了我一个错误。我还尝试了使用width=len(img[0])
看看是否会发现宽度更快,以防万一,但numpy占了上风。Timeit Times:Numpy拼接:0.18052252247208658
len():0.2773668664358264
@Halp你能把图像切成两半吗?我也想这样做,但我有这个错误。如何将其扩展到文件夹中的多个图像?
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)