问题的核心
PIL
没有全屏打开图像的本机方法。这是有道理的,它不能。 PIL 所做的只是在默认情况下打开您的文件.bmp
文件查看程序(通常是 Windows 上的 Windows Photos [尽管这取决于 Windows 版本])。为了使其全屏打开该程序,PIL 需要知道发送该程序的参数。没有标准语法。因此,这是不可能的。
但是,这并不意味着没有全屏打开图像的解决方案。通过使用 Python 中的本机库 Tkinter,我们可以创建自己的全屏显示图像的窗口。
兼容性
为了避免系统依赖(直接调用.dll和.exe文件)。这可以通过 Tkinter 来完成。 Tkinter 是一个显示库。该代码可以在任何运行 Python 2 或 3 的计算机上完美运行。
我们的职能
import sys
if sys.version_info[0] == 2: # the tkinter library changed it's name from Python 2 to 3.
import Tkinter
tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
import tkinter
from PIL import Image, ImageTk
def showPIL(pilImage):
root = tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.quit()))
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
imgWidth, imgHeight = pilImage.size
if imgWidth > w or imgHeight > h:
ratio = min(w/imgWidth, h/imgHeight)
imgWidth = int(imgWidth*ratio)
imgHeight = int(imgHeight*ratio)
pilImage = pilImage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(w/2,h/2,image=image)
root.mainloop()
Usage
pilImage = Image.open("colagem3.png")
showPIL(pilImage)
Output
它会创建一个全屏窗口,您的图像位于黑色画布的中心。如果需要,您的图像将被调整大小。这是它的视觉效果:
注意:使用 escape 关闭全屏