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
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.
since you used 'with', no close should be necessary
The image should close after the sleep, automatically
(May-01-2017, 04:00 PM)Larz60+ Wrote: [ -> ]since you used 'with', no close should be necessary
The image should close after the sleep, automatically

Yeah, it doesn't seem to be closing with or without 'close.' Not sure why.
It may be closed, but screen not refreshed.
If it was drawn on a canvas, try rewriting the canvas
(May-01-2017, 04:32 PM)Larz60+ Wrote: [ -> ]It may be closed, but screen not refreshed.
If it was drawn on a canvas, try rewriting the canvas



Sorry, I'm not sure what you mean by canvas. 
The images are screenshots, and are being opened by Windows Photo Viewer.
https://pillow.readthedocs.io/en/4.1.x/r...Image.show Wrote:Image.show(title=None, command=None)
Displays this image. This method is mainly intended for debugging purposes.

Emphasis mine.
There probably isn't meant to be a way to close it, since opening it is just for debugging to make sure things are working.
(May-01-2017, 08:17 PM)nilamo Wrote: [ -> ]
https://pillow.readthedocs.io/en/4.1.x/r...Image.show Wrote:Image.show(title=None, command=None)
Displays this image. This method is mainly intended for debugging purposes.

Emphasis mine.
There probably isn't meant to be a way to close it, since opening it is just for debugging to make sure things are working.


I see. Any suggestions as to what I should do?
I 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.
(May-02-2017, 04:01 PM)nilamo Wrote: [ -> ]I 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.


I have about 200 stock charts I want to look at, and I just need to look at them for a few seconds each. 
I want to automate this process.
Ok, I think I'd try to use pygame then.
Loading the image as a surface: https://www.pygame.org/docs/ref/image.ht...image.load
Once it's a surface, you can draw it to the screen just as any other surface in pygame, so this guide can help with the rest: https://python-forum.io/Thread-PyGame-Lo...ets-part-2
Pages: 1 2