Python Forum
Closing an image opened by Pillow in Window Photo Viewer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Closing an image opened by Pillow in Window Photo Viewer (/thread-3149.html)

Pages: 1 2


Closing an image opened by Pillow in Window Photo Viewer - bigmit37 - May-01-2017

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.


RE: Closing an image opened by Pillow in Window Photo Viewer - Larz60+ - May-01-2017

since you used 'with', no close should be necessary
The image should close after the sleep, automatically


RE: Closing an image opened by Pillow in Window Photo Viewer - bigmit37 - May-01-2017

(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.


RE: Closing an image opened by Pillow in Window Photo Viewer - Larz60+ - May-01-2017

It may be closed, but screen not refreshed.
If it was drawn on a canvas, try rewriting the canvas


RE: Closing an image opened by Pillow in Window Photo Viewer - bigmit37 - May-01-2017

(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.


RE: Closing an image opened by Pillow in Window Photo Viewer - nilamo - May-01-2017

https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.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.


RE: Closing an image opened by Pillow in Window Photo Viewer - bigmit37 - May-02-2017

(May-01-2017, 08:17 PM)nilamo Wrote:
https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.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?


RE: Closing an image opened by Pillow in Window Photo Viewer - nilamo - May-02-2017

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.


RE: Closing an image opened by Pillow in Window Photo Viewer - bigmit37 - May-02-2017

(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.


RE: Closing an image opened by Pillow in Window Photo Viewer - nilamo - May-02-2017

Ok, I think I'd try to use pygame then.
Loading the image as a surface: https://www.pygame.org/docs/ref/image.html#pygame.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-Loading-images-transparency-handling-spritesheets-part-2