需要注意的是,图像切片也没有创造的一个副本cropped image,但创建pointer的roi。如果要加载这么多图像,使用切片裁剪图像的相关部分,然后附加到列表中,这可能会造成巨大的内存浪费。
假设您每个都加载N个图像,>1MP并且您只需要100x100左上角的区域。
Slicing:X = []for i in range(N):
im = imread('image_i')
X.append(im[0:100,0:100]) # This will keep all N images in the memory.
# Because they are still used.
或者,您可以复制相关部分.copy(),因此垃圾收集器将删除im。X = []for i in range(N):
im = imread('image_i')
X.append(im[0:100,0:100].copy()) # This will keep all only the crops in the memory.
# im's will be deleted by gc.
找出在此之后,我意识到一个评论由user1270710提到,但我花了相当长的一段时间去找出(即,调试等)。所以,我认为值得一提。