如何打开 Python 中的图像并在之后关闭?
How to open an image in Python and close afterwards?
Python 版本:3.7.9
我试过将 Pillow/PIL 与 show() 和 close() 结合使用,但这只会关闭命令提示符并使文件查看器仍然打开。
我已经尝试过子进程 Popen,但即使文件是正确的,它也会给我错误 'The system cannot find the file specified'。所以我现在被困住了。我认为这对于 Python.
来说是一个简单的过程
我只是想打开一个图像,然后在用户单击按钮后关闭它。
打开 suggestions/libraries。谢谢!
只需调用此函数,就会弹出一个新的 window 图像。
在 window 关闭之前,您的代码不会被执行。
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
使用这个的示例行:
image_preview ("image.png", (500, 500))#With specified resolution
image_preview ("image.png")#Use the image resolution
Python 版本:3.7.9
我试过将 Pillow/PIL 与 show() 和 close() 结合使用,但这只会关闭命令提示符并使文件查看器仍然打开。
我已经尝试过子进程 Popen,但即使文件是正确的,它也会给我错误 'The system cannot find the file specified'。所以我现在被困住了。我认为这对于 Python.
来说是一个简单的过程我只是想打开一个图像,然后在用户单击按钮后关闭它。
打开 suggestions/libraries。谢谢!
只需调用此函数,就会弹出一个新的 window 图像。 在 window 关闭之前,您的代码不会被执行。
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
使用这个的示例行:
image_preview ("image.png", (500, 500))#With specified resolution
image_preview ("image.png")#Use the image resolution