python – Memory runs out when trying to convert images to numpy arrays


I have a list of about 15,000 images that are 120×90 pixels high and wide. I'm trying to convert them to a Numpy Array form, but when I try to convert them, my computer does not have enough memory (8 GB RAM + 12 GB swap). After this is done, I save it in a file for future machine learning exercises.

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

The function genDataSet () returns a list in the form of ((filepath1, genre), (filepath2, genre) …).

When this code runs, there is not enough memory left, so my computer freezes almost completely and I am forced to stop it.