Can I display image in full screen mode with PIL?
Question:
How to display image on full screen with Python Imaging Library?
from PIL import Image
img1 = Image.open ('colagem3.png');
img1.show ();
DISPLAY ON FULL SCREEN MODE!
Answers:
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.
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.
Compatibility
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.
Our function
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
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:
Note: use escape to close fullscreen
I know may be this is an old question but, I have exact same query. And this code is solving my 50% of the problem.
Another part of my problem is that, I am willing to add more images to the list and would like to do slide show. I am not able to do it. I have tried multiple options. Like:
Can I display image in full screen mode with PIL?
Please help to confirm how this code can be converted so that it can support multiple images for the slideshow.
How to display image on full screen with Python Imaging Library?
from PIL import Image
img1 = Image.open ('colagem3.png');
img1.show ();
DISPLAY ON FULL SCREEN MODE!
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.
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.
Compatibility
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.
Our function
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
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:
Note: use escape to close fullscreen
I know may be this is an old question but, I have exact same query. And this code is solving my 50% of the problem.
Another part of my problem is that, I am willing to add more images to the list and would like to do slide show. I am not able to do it. I have tried multiple options. Like:
Can I display image in full screen mode with PIL?
Please help to confirm how this code can be converted so that it can support multiple images for the slideshow.