Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load all images from a folder using PIL

import glob
from PIL import Image 


marked = glob.iglob("D:/Users/username/Desktop/cells/Marked")

img = Image.open(marked)
img.show()

I am trying to create a neural network using an image dataset contained in the Folder marked. When I run the code I get the error:

 File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2547, in open
    fp.seek(0)
AttributeError: 'generator' object has no attribute 'seek'

I do not understand what the error means, it seems to be misdirected?

like image 915
Nick Rizzolo Avatar asked May 27 '18 23:05

Nick Rizzolo


People also ask

How do I import photos from PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).


2 Answers

You'll need to specify a wildcard at the end of your path and iterate:

images = []
for f in glob.iglob("D:/Users/username/Desktop/cells/Marked/*"):
    images.append(np.asarray(Image.open(f)))

images = np.array(images)
like image 78
cs95 Avatar answered Oct 24 '22 15:10

cs95


See this answer, which uses PIL.Image and glob to find all images in the folder and load them into an array.

from PIL import Image
import glob
image_list = []
for filename in glob.glob('yourpath/*.gif'): #assuming gif
    im=Image.open(filename)
    image_list.append(im)
like image 26
generic_stackoverflow_user Avatar answered Oct 24 '22 15:10

generic_stackoverflow_user