如何使用python通过与文件夹中的文件名进行比较来显示图像?

2024-05-15 20:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的新学员,我在一个列表中有几个文件名,并且在一个文件夹中保存了许多具有相同文件名的图像,如下所示。你知道吗

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

我想将列表中的文件名与上面文件夹中图像的文件名逐一进行比较一个。如果它与图像将显示的相应名称匹配。 我试过贴在这里的东西。你知道吗

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])

请帮帮我。提前谢谢


Tags: pathin图像imageimport文件夹列表for
1条回答
网友
1楼 · 发布于 2024-05-15 20:48:48

我想我有你的问题了。如果我是对的,这是答案。你知道吗

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()

相关问题 更多 >