Reputation: 643
I am attempting to import a large number of images and convert them into an array to do similarity comparisons between images based on colors at each pixel and shapes contained within the pictures. I'm having trouble importing the data, the following code works for small numbers of images (10-20) but fails for larger ones (my total goal is to import 10,000 for this project).
from PIL import Image
import os,os.path
imgs=[]
path="Documents/data/img"
os.listdir(path)
valid_images =[".png"]
for f in os.listdir(path):
ext= os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
imgs.append(Image.open(os.path.join(path,f)))
When I execute this I receive the following message
OSError: [Errno 24] Too many open files: 'Documents/data/img\81395.png'
Is there a way to edit how many files can be open simultaneously? Or possibly a more efficient way to convert these tables to arrays as I go and "close" the image? I'm very new to this sort of analysis so any tips or pointers are appreciated.
Upvotes: 0
Views: 759
Reputation: 2406
Don't store PIL.Image objects and just convert them into numpy arrays instead. For that you need to change the line where you append image to a list to this:
'''
imgs.append(np.asarray(Image.open(os.path.join(path,f))))
'''
Upvotes: 1