我可以使用 PIL 以全屏模式显示图像吗?
Can I display image in full screen mode with PIL?
如何使用 Python 图像库全屏显示图像?
from PIL import Image
img1 = Image.open ('colagem3.png');
img1.show ();
在全屏模式下显示!
问题核心
PIL
没有全屏打开图像的本机方法。它不能这样做是有道理的。 PIL 所做的只是在默认的 .bmp
文件查看程序中打开您的文件(通常,Windows Photos on Windows [虽然这是 Windows 版本相关]。为了全屏打开该程序,PIL 需要知道发送该程序的参数。没有标准的语法。所以,这是不可能的。
但是,这并不意味着没有全屏打开图像的解决方案。通过使用 Python、Tkinter 中的本机库,我们可以创建自己的 window 全屏显示图像。
兼容性
为了避免依赖系统(直接调用.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()
用法
pilImage = Image.open("colagem3.png")
showPIL(pilImage)
输出
它创建一个全屏 window,您的图像以黑色为中心 canvas。如果需要,您的图像将被调整大小。这是它的视觉效果:
注意:使用escape关闭全屏
如何使用 Python 图像库全屏显示图像?
from PIL import Image
img1 = Image.open ('colagem3.png');
img1.show ();
在全屏模式下显示!
问题核心
PIL
没有全屏打开图像的本机方法。它不能这样做是有道理的。 PIL 所做的只是在默认的 .bmp
文件查看程序中打开您的文件(通常,Windows Photos on Windows [虽然这是 Windows 版本相关]。为了全屏打开该程序,PIL 需要知道发送该程序的参数。没有标准的语法。所以,这是不可能的。
但是,这并不意味着没有全屏打开图像的解决方案。通过使用 Python、Tkinter 中的本机库,我们可以创建自己的 window 全屏显示图像。
兼容性
为了避免依赖系统(直接调用.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()
用法
pilImage = Image.open("colagem3.png")
showPIL(pilImage)
输出
它创建一个全屏 window,您的图像以黑色为中心 canvas。如果需要,您的图像将被调整大小。这是它的视觉效果:
注意:使用escape关闭全屏