53

I am trying to import all the images inside a directory (the directory location is known).

path = /home/user/mydirectory

I already know a way of finding out the length of the directory.

What I'm not sure about is how I can import the images (using PIL/Pillow) into either a list or a dictionary, so they can be properly manipulated.

The Dan
  • 602
  • 4
  • 18
Charles
  • 589
  • 1
  • 4
  • 8
  • 2
    You might find this helpful: http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python It's a method for listing all file in a directory, from there you could just call your read image function on each. – Mike Oct 15 '14 at 21:33
  • 1
    Mike gave you a solution to find all the files - since you have all the file names, you can open them - sometimes you have to think! – Tony Suffolk 66 Oct 15 '14 at 21:35
  • hey Charles, you're pretty new here and so am I...one thing I've learned is that the easier it is for people to help you, the more feedback you'll get. It's OK (IMHO) to not know how to program and still ask a question but to demonstrate effort by coming up with even a broken version of code will make folks happier to help. We here like examples that we can fix, not generalities! I think it's a bit mean to downvote a new user so don't be discouraged! – user1269942 Oct 15 '14 at 21:44
  • 1
    @AndrewBarber : With all due respoect to your moderator role, I think the question is clear (and shouldn't be on hold) - given that the question has two answers (which do similar things), and one of which has been accepted. – Tony Suffolk 66 Oct 16 '14 at 07:30
  • This question is not at all clear, and the solutions do solve the problem only to a certain degree. – GhostCat Apr 15 '17 at 19:39

2 Answers2

78

I'd start by using glob:

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)

then do what you need to do with your list of images (image_list).

user1269942
  • 3,356
  • 20
  • 33
  • 7
    `image_list = map(Image.open, glob('your/path/*.gif'))` – jfs Oct 16 '14 at 03:21
  • 2
    A nicer solution than mine (using glob correctly), but this is more difficult to extend for other image types - unless you want a more complex regex. – Tony Suffolk 66 Oct 16 '14 at 07:14
  • both comments good. JFS: nice and compact...I would use something similar but sometimes it's easier for folks still learning python to understand something more readable, so that's what I try and deliver. Tony, yes, no matter how you slice it there will be a little bit of ugliness with accommodating for different extensions. – user1269942 Oct 16 '14 at 07:37
  • 1
    @J.F.Sebastian `image_list = [Image.open(item) for i in [glob.glob('your/path/*.%s' % ext) for ext in ["jpg","gif","png","tga"]] for item in i]` ;) – user1269942 Oct 16 '14 at 08:01
  • 1
    one more thing to note...sometimes I deal with 1,000s or 10s of 1,000s of images and to naively put them ALL into a list would be foolish. if I iterate with a for loop it makes it easier to chunk them into batches and operate withing my RAM limits. – user1269942 Aug 20 '16 at 18:47
  • Is there anyway to load them in Jupyter Notebook as output? I used 'image.show()` but it only opens image using a default app. – Bowen Liu Oct 31 '18 at 03:08
  • @BowenLiu try using matplotlib with the "%matplotlib inline" directive at the top of your notebook. see: https://stackoverflow.com/questions/19410042/how-to-make-ipython-notebook-matplotlib-plot-inline for details – user1269942 Oct 31 '18 at 19:18
34
from PIL import Image
import os, os.path

imgs = []
path = "/home/tony/pictures"
valid_images = [".jpg",".gif",".png",".tga"]
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)))
   
The Dan
  • 602
  • 4
  • 18
Tony Suffolk 66
  • 7,739
  • 2
  • 26
  • 33
  • @J.F.Sebastian - thanks for the corrections - I would have been happy for you to have made those edits :-) – Tony Suffolk 66 Oct 16 '14 at 07:12
  • another way to test it: `if f.lower().endswith(valid_images): append` (`valid_images` should be a tuple in this case). – jfs Oct 16 '14 at 07:25
  • @J.F.Sebastian : True in terms of using `endswith`, but I think it is *better* to use splitext and then compare when testing the extension of a file - it is a bit more explicit. – Tony Suffolk 66 Oct 16 '14 at 07:31
  • 1
    Related for checking if it is a valid image: https://stackoverflow.com/questions/889333/how-to-check-if-a-file-is-a-valid-image-file – lucidbrot Jul 15 '19 at 22:22