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
文件是一个示例,需要用您自己的图像文件替换。
height,width,_ = img.shape
,s1 = img [:,:width_cutoff,:]
和s2 = img [:,width_cutoff:,:]
。由于图像是二维的,程序在我删除这些内容之前一直给出错误提示。 - Elliot Killickwidth = len(img[0])
来查找宽度是否更快,但是 numpy 胜出了。Timeit 时间:
Numpy Splicing: 0.18052252247208658
len(): 0.2773668664358264
- Elliot Killickimport 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)
def imCrop(x):
height,width,depth = x.shape
return [x[height , :width//2] , x[height, width//2:]]
然后,您可以通过以下方式简单地绘制图像的右侧:
plt.imshow(imCrop(yourimage)[1])