10
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?

cs95
  • 379,657
  • 97
  • 704
  • 746
Nick Rizzolo
  • 125
  • 1
  • 2
  • 12
  • [`Image.open()`](https://pillow.readthedocs.io/en/5.1.x/reference/Image.html#PIL.Image.open) take a single file string/path/object. – wwii May 27 '18 at 23:53

2 Answers2

8

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)
cs95
  • 379,657
  • 97
  • 704
  • 746
4

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)