尝试将图像转换为numpy数组时内存不足

2024-09-28 18:55:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大约15000张图片的列表,高120x90像素,宽120x90像素。我试图将它们转换成Numpy数组形式,但是当我试图转换它们时,我的计算机内存不足(8GB的ram+12GB的交换)。完成后,我将保存到一个文件,以便将来进行机器学习培训。你知道吗

dataSet = genDataSet()
for image in dataSet:
    pixelImages.append([imageToRGB(image[0], True),image[1]])

def imageToRGB(inputFile, normalise = False):
    os.chdir("/home/spchee/CodeProjects/School Project/images")
    img = Image.open(inputFile) #Opens File
    pixels = np.asarray(img) #Converts it to a numpy array
    pixels = np.rint(pixels)
    if normalise: #This normalises it between the values of 0 and 1
        pixels= pixels/255
    img.close()
    return pixels

函数genDataSet()以[[filepath1,genre],[filepath2,genre]…]的形式返回一个列表。你知道吗

当运行这段代码时,它会耗尽内存,所以我的电脑几乎完全冻结,我被迫停止它。你知道吗


Tags: image列表imgnp图片it像素dataset