Python Forum

Full Version: Closing an image opened by Pillow in Window Photo Viewer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
You can do it like this,i use use webbrowser module.
If not given a url but a path it open my default image viewer(IrfanView) on Windows.
So it will show image for 3-sec and close IrfanView ect..
I do this with subprocess module.
import webbrowser
import subprocess
import os, time

for file in os.scandir('.'):
    if file.name.endswith(('.jpg', '.png')):
        webbrowser.open(file.name)
        time.sleep(3)
        subprocess.run(['taskkill', '/f', '/im', 'i_view32.exe'])
Output:
E:\1py_div\image_loop λ python img.py SUCCESS: The process "i_view32.exe" with PID 15912 has been terminated. SUCCESS: The process "i_view32.exe" with PID 20024 has been terminated. SUCCESS: The process "i_view32.exe" with PID 7376 has been terminated. SUCCESS: The process "i_view32.exe" with PID 11028 has been terminated. SUCCESS: The process "i_view32.exe" with PID 13252 has been terminated.
You see it close process,you most find process name for Windows Photo Viewer.
nilamo Wrote:nilamoI guess it depends on what you're trying to accomplish.  Why do you want to flash some images on the screen and immediately close them?

Since you're using the PIL already, there's ImageTk, which can display images in a Tk window: http://effbot.org/imagingbook/imagetk.htm
Or you can use something like pygame to open a few windows and blit the images, or use Tk directly, etc.
Thank you. I will take at look at this. =)


Thanks. I got it to work with subprocess but I ran it with this code:
import subprocess
for i in Final_Bioteck[6:11]:
    p = subprocess.Popen(["C:\Program Files\IrfanView\i_view64.exe", '{}.png'.format(i)])
    time.sleep(3)
    p.kill()
As for your code, I don't quite understand the first three agruments of
subprocess.run(['taskkill', '/f', '/im', 'i_view32.exe'])
What is taskkill, /f and /im
I briefly looked at the docs, and saw the arguments as (args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False)
but still not quite sure what your args mean.
Thank you.

I just tried out your code with some adjustments but it doesn't seem to be closing the opened windows.
import webbrowser
import subprocess
import os, time
 
for i in Final_Bioteck[6:11]:
        webbrowser.open( '{}.png'.format(i))
        time.sleep(3)
        subprocess.run(['taskkill', '/f', '/im', "C:\Program Files\IrfanView\i_view64.exe"]) 
Anything obvious I'm might be doing wrong?  I'm assuming my arguments for the last line are incorrect?
bigmit37 Wrote:What is taskkill, /f and /im? 
Taskkill ends one or more tasks or processes,subprocess call this.

bigmit37 Wrote:but still not quite sure what your args mean.
args is what to call in this case Taskkill.

bigmit37 Wrote:I'm assuming my arguments for the last line are incorrect?
It's not path but process name 
Open Task Manager and find the process.
It should probably be:
subprocess.run(['taskkill', '/f', '/im', "i_view64.exe"])
You can not remove os.scandir('.'),
and use a ordinary loop.
We are working with files on OS now.
(May-02-2017, 07:47 PM)snippsat Wrote: [ -> ]
bigmit37 Wrote:What is taskkill, /f and /im? 
Taskkill ends one or more tasks or processes,subprocess call this.

bigmit37 Wrote:but still not quite sure what your args mean.
args is what to call in this case Taskkill.

bigmit37 Wrote:I'm assuming my arguments for the last line are incorrect?
It's not path but process name 
Open Task Manager and find the process.
It should probably be:
subprocess.run(['taskkill', '/f', '/im', "i_view64.exe"])
You can not remove os.scandir('.'),
and use a ordinary loop.
We are working with files on OS now.


Got it working with this code
'import webbrowser
import subprocess
import os, time
  
for i in Final_Bioteck[6:11]:
        webbrowser.open( '{}.png'.format(i))
        time.sleep(3)
        subprocess.run(['taskkill', '/f', '/im', "i_view64.exe"]) 
Yeah, my i_view is 64 bit, that's what was causing the malfunction of the code. 
My original loop seems to work fine though.

I'm still not quite sure what ''/f"   and '/im' stand for though.   Sad 


Thank you so much.
Quote:Thanks. I got it to work with subprocess but I ran it with this code
Okay i see,so Final_Bioteck[6:11]: is not reading from disk.
Is Final_Bioteck a list of file names?
I can do it like this reading files in a folder.
import subprocess
import os, time

for file in os.scandir('.'):
    if file.name.endswith(('.jpg', '.png')):
        p = subprocess.Popen(['C:/Program Files (x86)/IrfanView/i_view32', file.name])
        time.sleep(3)
        p.kill()
So i guess that this should work for you.
import subprocess
import os, time

for i in Final_Bioteck[6:11]:
    p = subprocess.Popen(["C:/Program Files/IrfanView/i_view64.exe", '{}.png'.format(i)])
    time.sleep(3)
    p.kill()
Quote:I'm still not quite sure what ''/f"   and '/im' stand for though.
It's in the link i posted.
/f   Specifies that process(es) be forcefully terminated.
/im   ImageName   Specifies the image name of the process to be terminated.
(May-03-2017, 12:01 AM)snippsat Wrote: [ -> ]
Quote:Thanks. I got it to work with subprocess but I ran it with this code
Okay i see,so Final_Bioteck[6:11]: is not reading from disk.
Is Final_Bioteck a list of file names?
I can do it like this reading files in a folder.
import subprocess
import os, time

for file in os.scandir('.'):
    if file.name.endswith(('.jpg', '.png')):
        p = subprocess.Popen(['C:/Program Files (x86)/IrfanView/i_view32', file.name])
        time.sleep(3)
        p.kill()
So i guess that this should work for you.
import subprocess
import os, time

for i in Final_Bioteck[6:11]:
    p = subprocess.Popen(["C:/Program Files/IrfanView/i_view64.exe", '{}.png'.format(i)])
    time.sleep(3)
    p.kill()
Quote:I'm still not quite sure what ''/f"   and '/im' stand for though.
It's in the link i posted.
/f   Specifies that process(es) be forcefully terminated.
/im   ImageName   Specifies the image name of the process to be terminated.


Ah sorry :(
I didn't even see the link. 

Thank you.
(May-01-2017, 03:49 PM)bigmit37 Wrote: [ -> ]I'm trying to close each image opened via iteration, within each iteration.




for i in Final_Bioteck[:5]:
     with Image.open('{}_screenshot.png'.format(i)) as test_image:
        test_image.show()
        time.sleep(2)
 My code doesn't seem to close the photos opened via Windows Photo Viewer. 
I also tried,



test_image.close()

 , but no result.





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


Thank you.


I think, you must choose an appropriate viewer instead like a PhotoViewerPro program. It might be suitable because Pillow provides only a view method which attempts to detect your OS.
Pages: 1 2