S.Akhil S.Akhil - 28 days ago 14
Python Question

How to display images by comparing with their filenames from folder using python?

I am the new learner to Python.I have few filenames in a list and saved lots images with the same filenames in a folder as shown below.

file_name_list=['Screenshot (1)','Screenshot (10)','Screenshot (11)','Screenshot (12)','Screenshot (13)','Screenshot (14)']
folder_path="C:\\Users\\akhil kumar\\Desktop\\images\\"


I want to compare filename in the list with filenames of images in the above folder one by one.If it matches with the respective name the image will display.
I have tried something posted here.

import os
from IPython.display import Image
file_list = []
for root, dirs, files in os.walk(path):
for file in files:
a=os.path.splitext(os.path.basename(file))[0]
file_list.append(a)
p = [0,1,2,3,4,5]
for i in p:
if b[i]==file_list[i]:
Image(filename= b[i])


Please help me in this. thanks in advance

Answer

I got your question I guess. If I am correct, Here is the answer.

path= "Set/Path"
a=['a', 'b', 'c', 'd', 'e']

import os
from IPython.display import Image
from PIL import Image as PImage

file_list = []
for root, dirs, files in os.walk(path):
    for file in files:
        b=os.path.splitext(os.path.basename(file))[0]
        file_list.append(b)



import os
c = []
for root, dirs, files in os.walk(path):
    for file in files:
        d=os.path.basename(file)
        c.append(d)

loadedImages = []
for i in range(len(a)):
    for j in range(len(file_list)):
        if a[i]== file_list[j]:
            #Image(filename=c[],width=200, height=250, unconfined=True) 
            img = PImage.open(c[j])
            loadedImages.append(img)
for img in loadedImages:
    img.show()