我可以使用PIL在全屏模式下显示图像吗? [英] Can I display image in full screen mode with PIL?
问题描述
如何使用Python Imaging Library在全屏上显示图像?
How to display image on full screen with Python Imaging Library?
from PIL import Image
img1 = Image.open ('colagem3.png');
img1.show ();
在全屏模式下显示!
推荐答案
问题的核心
PIL
没有本机打开全屏图像的方法.这是有道理的. PIL所做的只是在默认的.bmp
文件查看程序中打开文件(通常,Windows上的Windows Photos [尽管这取决于Windows版本]).为了使其全屏打开该程序,PIL需要知道发送该程序的参数.没有标准语法.因此,这是不可能的.
Core of the problem
PIL
has no native way of opening an image in full screen. And it makes sense that it can't. What PIL does is it simply opens your file in the default .bmp
file viewing program (commonly, Windows Photos on Windows [although this is Windows version dependent]). In order for it to open that program in full screen, PIL would need to know what arguments to send the program. There is no standard syntax for that. Thus, it is impossible.
但是,这并不意味着没有解决方案来全屏打开图像.通过使用Python中的本机库Tkinter,我们可以创建自己的窗口,该窗口全屏显示并显示图像.
But, that doesn't mean that there isn't a solution to opening images in fullscreen. By using a native library in Python, Tkinter, we can create our own window that displays in fullscreen which shows an image.
为了避免依赖系统(直接调用.dll和.exe文件).这可以用Tkinter完成. Tkinter是一个显示库.该代码将在运行Python 2或3的任何计算机上完美运行.
In order to avoid being system reliant (calling .dll and .exe files directly). This can be accomplished with Tkinter. Tkinter is a display library. This code will work perfectly on any computer that runs Python 2 or 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)
输出
它将创建一个全屏窗口,其中图像以黑色画布为中心.如果需要,您的图像将被调整大小.这是它的视觉效果:
Output
It creates a fullscreen window with your image centered on a black canvas. If need be, your image will be resized. Here's a visual of it:
注意:使用转义符关闭全屏显示
这篇关于我可以使用PIL在全屏模式下显示图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!