I want to display a gif image using Pillow
Here is my simple code:
from tkinter import *
from PIL import Image, ImageTk
import tkinter as Tk
image = Image.open("Puissance4.gif")
image.show()
But nothing happens...
All help will be appreciated
Thanks!
PIL provides a show method which attempts to detect your OS and choose an
appropriate viewer. On Unix it tries calling the imagemagick command display
or xv. On Macs it uses open, on Windows it uses... something else.
If it can't find an appropriate viewer, ImageShow._viewers will be an empty list.
On Raspbian, you'll need to install an image viewer such as display, xv or fim. (Note a search on the web will show that there are many image viewers available.) Then
you can tell PIL to use it by specifying the command parameter:
image.show(command='fim')
To display an image in Tkinter, you could use something like:
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
img = Image.open("image.gif")
tkimage = ImageTk.PhotoImage(img)
tk.Label(root, image=tkimage).pack()
root.mainloop()
Related
instead of using a ico file or blank icon in my tkinter application, i want the tkinter icon to be a 16x16 square with a certain color.
my current code:
import tkinter as tk
from tkinter import *
root = tk.Tk()
icon=PhotoImage(height=16, width=16)
icon.blank()
root.wm_iconphoto('True', icon) #New Tk 8.6 style
root.geometry('707x267')
root.title(" ")
root.mainloop()
how do i change the color of the icon?
You can create your own image with an image processing library, like PIL. First install it:
pip install Pillow
Then create a new image with:
from PIL import Image, ImageTk
img = Image.new(mode='RGB',size=(16,16),color='red')
Now convert the image into tkinter understandable format with:
img_tk = ImageTk.PhotoImage(img)
Next, set the icon to be that new image:
root.wm_iconphoto('True', img_tk)
When uploading an image to a label in Tkinter, it appears black. Not white or blank like some other questions, I did check them, and although I may have missed something, none of the answers there worked for me.
Essentially, I upload an image. It appears, however, it displays only a black image, instead of the image I was using. Here is my code:
#Imports are here
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from PIL import Image, ImageTk
#Retrieving and opening image
img = ImageTk.PhotoImage(Image.open("AFile.ppm"))
label = Label(frametwo, image=img)
label.photo = img
label.grid(column=2,row=2)
This does not display an errors, the image just shows up weird. I have no clue as to why. Thanks in advance.
from tkinter import *
from tkinter import ttk
win = Tk()
colorful=PhotoImage(file='image/1.png')
_tkinter.TclError: couldn't recognize data in image file "image/1.png" file path is correct, from beginning it was running without an error but when I add some more code it happened, why?
Try this:
from PIL import Image, ImageTk
img = Image.open("file_name")
colorful = ImageTk.PhotoImage(img)
Tkinter Photoimage only supports .gif images.
If you want to display .png images, Use PIL (Python Imaging Library)
Fore more info, see https://pythonbasics.org/tkinter-image/ .
In using Python's Tkinter application, I have come across a little problem. How do I hide and show GIF images that I have put into the window using PhotoImage?
I can make the images appear in the first place, but am unable to do anything with them. I have tried using canvas.itemconfig and canvas.update, but to no avail. Is anyone able to solve this problem?
picture = PhotoImage(file='C:\\Users\\ZecFamily5\\Downloads\\Island.gif')
c.create_image(250, 250, image=picture)
The above code cannot be hidden or shown using c.itemconfig(image, state=HIDDEN).
You should do .itemconfig(Your Item Name, state=HIDDEN) and to show, change HIDDEN to NORMAL to show.
I would recommend using the ImageTK module:
img = ImageTk.PhotoImage(Image.open(path))
As it should allow you to make a TKinter compatible photo image.
For this to work though, do keep in mind that this will need to be at the beginning of your file:
import Tkinter as tk
from PIL import ImageTk, Image
Here is an example piece of code to show you what I mean:
import Tkinter as tk
from PIL import ImageTk, Image
path = 'C:/xxxx/xxxx.jpg'
root = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
I have been trying to resize images using PIL then display them using Tkinter, but the program has been crashing and I've isolated the problem to the second line below:
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
And this is my imports:
from Tkinter import *
from PIL import Image, ImageTk
I've read around that Tk must be initialized and I do this in the program before it reaches those lines in the program. So I don't know what it is.
I am running OSX and python 2.7 interpreter on eclipse (using PyDev).
UPDATE:
Error message on eclipse says:
STACK: Stack after current is in use
I've seen that error before using tkinter. I think it had something to do with an older version of tkinter. I updated my python version and tkinter version and it went away. Does this error happen when you run your code on a different OS/Computer/Platform/Version of Python? What version of tkinter are you using? Some google searching revealed these two pages which describe the same bug while using tkinter...
http://osdir.com/ml/python.leo.general/2008-03/msg00060.html
http://fornax.phys.unm.edu/lwa/trac/ticket/3
I can't see all your code, but I'm betting that there is not necessarily anything wrong with your code. The following code worked for me...
from Tkinter import *
from PIL import Image, ImageTk
# resize image with PIL
im = Image.open('path to gif')
resized_im = im.resize((400,400,),Image.ANTIALIAS)
# display image in tkinter window
window = Tk()
tk_im = ImageTk.PhotoImage(resized_im)
window.geometry('%dx%d' % (resized_im.size[0],resized_im.size[1]))
label_image = Label(window, image=tk_im)
label_image.place(x=0,y=0,width=resized_im.size[0],height=resized_im.size[1])
window.mainloop()
Using....
ubuntu 10.04 64 bit
python 2.6.5
python-imaging-tk 1.1.7
python-tk 2.6.5 (which uses version 8.5.0 of tkinter)
python imaging library (PIL) 1.1.7
eclipse 3.7.1
pydev 2.5.0.2012050419
Good luck!
I've been using both Tk, PIL and resizing images for a current project and the following code works fine for me.
#Imports
from Tkinter import *
from PIL import Image, ImageTk
#Create Tk instance
root = Tk()
#Open image and resize
image = Image.open("path/to/image/file").resize((400,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
After that, I find it easiest to display the images as labels in tkinter like so.
image_label = Label(root, width = 400, height = 400, image = photo bd = 0)
(I like the bd = 0 as otherwise I get a thin white border around the image.)
Hope this has helped you. Good luck!
Ed
So this is an ancient question, but in case someone stumbles upon this (like I just did), the error message is from Tcl (tclExecute.c). I have no idea what's triggering it, but one thing worth trying is to create a Tk instance before calling PhotoImage:
root = Tk()
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)