2017-05-01 31 views
1

我試圖關閉在每次迭代中通過迭代打開的每個圖像。在窗口照片查看器中關閉由枕頭打開的圖像

我在下面提到了這個線程,但正確的答案是不產生結果。

How do I close an image opened in Pillow?

我的代碼

for i in Final_Bioteck[:5]: 
    with Image.open('{}_screenshot.png'.format(i)) as test_image: 
     test_image.show() 
     time.sleep(2) 

我也試過,

test_image.close(),但沒有結果。

我上面的循環打開了5個Windows Photos Viewer對話框;我希望通過迭代,每個窗口都會關閉。

我也看到了這個主題,但答案相當過時,所以不知道是否有更簡單的方法來執行我的願望。 How can I close an image shown to the user with the Python Imaging Library?

謝謝=)

回答

2

得到它的工作,但我在Windows上安裝不同的圖像瀏覽器,因爲我無法找到默認瀏覽器的.exe文件。

import webbrowser 
import subprocess 
import os, time 

for i in Final_Bioteck[6:11]: 
     webbrowser.open('{}.png'.format(i)) # opens the pic 
     time.sleep(3) 
     subprocess.run(['taskkill', '/f', '/im', "i_view64.exe"]) 

#taskkill kills the program, '/f' indiciates it's a process, '/'im' (not entirely sure), and i_view64.exe is the image viewers' exe file.