2

i'm trying to retrieve images from my local directory using array list but i got all images from list but i don't know how to show images from list one by one. Please help me if anyone knows.

my.py:

import os, os.path

imgs = []
# print(imgs)
path = "C:/Users/admin/PycharmProjects/my_module/static/files"
valid_images = [".jpg",".gif",".png",".tga"]
print(valid_images)
for f in os.listdir(path):
    ext = os.path.splitext(f)[1]
    os.listdir(path).append(f)
    # Image.show(f)
Sruthipriyanga
  • 468
  • 4
  • 17

3 Answers3

1

There are several options depending on your environment and visualization libraries. Check this good answer

Hanch
  • 35
  • 4
1

You need to do from PIL import Image, then you could do Image.open(path + "/" + f).show(). Try this:

import os, os.path
from PIL import Image

imgs = []

path = "path/to/directory" 

valid_images = [".jpg", ".gif", ".png", ".tga"]
print(valid_images)

for f in os.listdir(path):   
    ext = os.path.splitext(f)[1]
    if ext in valid_images:
        imgs.append(path + "/" + f)
    img = Image.open(path + "/" + f)
    img.show()

print(imgs)
srikavineehari
  • 2,502
  • 1
  • 11
  • 21
  • Sir., i want to fetch only one image based on the condition. for example if the count is 3 i want to fetch any one image from my local directory... i'm trying but i don't know how to fetch only one element.. can you help me sir – Sruthipriyanga Dec 27 '17 at 06:20
1

I have tried and i got my expected output. py:

  import os
    from PIL import Image
    img=[]
    path="C:/Users/admin/PycharmProjects/my_module/static/files/"
    for f in os.listdir(path):
        ext = os.path.splitext(f)[1]
        os.listdir(path).append(f)
        image = Image.open(f)
        image.show()
Sruthipriyanga
  • 468
  • 4
  • 17