1

I'm trying to close each image opened via iteration, within each iteration.

I've referred to this thread below, but the correct answer is not producing the results.

How do I close an image opened in Pillow?

My code

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

I also tried,

test_image.close() , but no result.

My loop above is opening 5 Windows Photos Viewer dialogs; I was hoping that through iteration, each window would be closed.

I saw this thread as well, but the answers are pretty outdated, so not sure if there is a more simple way to execute what I desire. How can I close an image shown to the user with the Python Imaging Library?

Thank you =)

Community
  • 1
  • 1
Moondra
  • 4,399
  • 9
  • 46
  • 104

2 Answers2

2

Got it working, but I installed a different image viewer on Windows as I couldn't find the .exe of the default viewer.

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. 
Moondra
  • 4,399
  • 9
  • 46
  • 104
0

In Windows 10, the process is dllhost.exe using the same script as Moondra, except with "dllhost.exe" instead of "i_view64.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', "dllhost.exe"])  

#taskkill kills the program, '/f' indicates it's a process, '/'im' (indicates the name of the image process), and "dllhost.exe" is the image viewers' exe file.