Python Forum
Closing an image opened by Pillow in Window Photo Viewer
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Closing an image opened by Pillow in Window Photo Viewer
#11
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.
Reply
#12
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?
Reply
#13
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.
Reply
#14
(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.
Reply
#15
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.
Reply
#16
(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.
Reply
#17
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,613 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  aoigram, pil Help with photo processing kolpac21 0 529 Aug-07-2023, 04:59 PM
Last Post: kolpac21
  does not save in other path than opened files before icode 3 833 Jun-23-2023, 07:25 PM
Last Post: snippsat
  Tkinter Pillow Resize Image Looks Choppy While Resizing AaronCatolico1 2 1,326 Jan-03-2023, 05:41 PM
Last Post: AaronCatolico1
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,132 Jun-13-2022, 08:59 AM
Last Post: Shena76
Question How to get html information from a tab of my default browser opened with webbrowser? noahverner1995 2 4,338 Jan-14-2022, 10:02 AM
Last Post: noahverner1995
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,670 Jan-08-2022, 12:13 PM
Last Post: law
  Rmarkdown opened by python code - errors Rav013 0 2,049 Apr-27-2021, 03:13 PM
Last Post: Rav013
  Invert Pillow image colours chesschaser 5 3,606 Jul-11-2020, 02:59 PM
Last Post: chesschaser
  Add Photo in Python adninqasifa 4 5,814 Nov-26-2018, 06:06 PM
Last Post: adninqasifa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020