0

Python Versions: 3.7.9

I've tried using Pillow/PIL with show() and close() however that just closes the command prompt and leaves the file viewer still open.

I've tried subprocess Popen but that just gives me the error 'The system cannot find the file specified' even though the file is correct. So I'm just stuck right now. I figured this would've been a simple process for Python.

I'm just trying to open an image and then after the user clicks a button, be able to close it.

Open to suggestions/libraries. Thanks!

Mason
  • 25
  • 6
  • Did you check out this thread already? --> https://stackoverflow.com/questions/31751464/how-do-i-close-an-image-opened-in-pillow/31751501 – Maximilian Freitag Oct 14 '21 at 20:20
  • @MaximilianFreitag Yeah, the test.close() doesn't close the file previewer unfortunately which is what I'd like it to do – Mason Oct 14 '21 at 20:23

1 Answers1

0

Just call this function and a new window will pop up with the image. Your code wont be executed until the window is closed.

import tkinter
from PIL import ImageTk, Image

def image_preview (path, size = (0, 0)):
    root = tkinter.Tk ()#Start window
    root.title ("Preview")#Modify this to change the title
    rawimg = Image.open(path)#Open image path

    if size != (0, 0):#If a specific resolution has been requested
        rawimg = rawimg.resize (size)#Change the image resolution

    img = ImageTk.PhotoImage(rawimg)#Adapt the image to tkinter
    panel = tkinter.Label (root, image = img)#Add image to the window
    panel.pack ()#Build window
    root.mainloop ()#Run window

Example line to use this:

image_preview ("image.png", (500, 500))#With specified resolution
image_preview ("image.png")#Use the image resolution
Sagitario
  • 102
  • 8
  • I tried this and it does open the image, however it opens it VERY zoomed in hah – Mason Oct 18 '21 at 13:44
  • I have update the answer, now you can pass a resolution as a tuple to the function. The specified resolution will be the size of the image and window. I have also added comments. – Sagitario Oct 18 '21 at 18:44