2016-11-09 1 views
1

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

도와주세요. 미리 감사드립니다.

+0

무엇이 잘못 되었나요? 표시되는 오류 메시지 또는 잘못된 동작이 무엇입니까? 어떤 도움이 필요 하신가요? – darthbith

+0

"file_name_list"의 이름과 폴더의 이미지 이름이 일치하면 해당 이미지가 표시됩니다. –

답변

1

나는 당신의 질문을 가지고 있습니다. 내가 옳다면, 여기에 답이 있습니다.

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

감사합니다. –

관련 문제