How to add an image in Tkinter?

Question:

How do I add an image in Tkinter?

This gave me a syntax error:

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()
Asked By: Damien

||

Answers:

There is no “Syntax Error” in the code above – it either ocurred in some other line (the above is not all of your code, as there are no imports, neither the declaration of your path variable) or you got some other error type.

The example above worked fine for me, testing on the interactive interpreter.

Answered By: jsbueno

Following code works on my machine

  1. you probably have something missing in your code.
  2. please also check the code files’s encoding.
  3. make sure you have PIL package installed

    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()
    
Answered By: Takahiro

Python 3.3.1 [MSC v.1600 32 bit (Intel)] on win32 14.May.2013

This worked for me, by following the code above

from tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()
img = ImageTk.PhotoImage(Image.open("True1.gif"))
panel = Label(root, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
root.mainloop()
Answered By: josav09

It’s not a standard lib of python 2.7. So in order for these to work properly and if you’re using Python 2.7 you should download the PIL library first:

https://pillow.readthedocs.io/en/latest/installation.html

After installing it, follow these steps:

  1. Make sure that your script.py is at the same folder with the image you want to show.

  2. Edit your script.py

    from Tkinter import *        
    from PIL import ImageTk, Image
    
    app_root = Tk()
    
    #Setting it up
    img = ImageTk.PhotoImage(Image.open("app.png"))
    
    #Displaying it
    imglabel = Label(app_root, image=img).grid(row=1, column=1)        
    
    
    app_root.mainloop()
    

Hope that helps!

Answered By: karlzafiris

Here is an example for Python 3 that you can edit for Python 2 😉

from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os

root = Tk()
root.geometry("550x300+300+150")
root.resizable(width=True, height=True)

def openfn():
    filename = filedialog.askopenfilename(title='open')
    return filename
def open_img():
    x = openfn()
    img = Image.open(x)
    img = img.resize((250, 250), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)
    panel = Label(root, image=img)
    panel.image = img
    panel.pack()

btn = Button(root, text='open image', command=open_img).pack()

root.mainloop()

enter image description here

Answered By: Walid Bousseta

It’s a Python version problem. If you are using the latest, then your old syntax won’t work and give you this error. Please follow @Josav09’s code and you will be fine.

Answered By: Akash Thakur

Your actual code may return an error based on the format of the file path points to. That being said, some image formats such as .gif, .pgm (and .png if tk.TkVersion >= 8.6) is already supported by the PhotoImage class.

Below is an example displaying:

Lenna (.png)

or if tk.TkVersion < 8.6:

Lenna (.gif)

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def download_images():
    # In order to fetch the image online
    try:
        import urllib.request as url
    except ImportError:
        import urllib as url
    url.urlretrieve("https://i.stack.imgur.com/IgD2r.png", "lenna.png")
    url.urlretrieve("https://i.stack.imgur.com/sML82.gif", "lenna.gif")


if __name__ == '__main__':
    download_images()
    root = tk.Tk()
    widget = tk.Label(root, compound='top')
    widget.lenna_image_png = tk.PhotoImage(file="lenna.png")
    widget.lenna_image_gif = tk.PhotoImage(file="lenna.gif")
    try:
        widget['text'] = "Lenna.png"
        widget['image'] = widget.lenna_image_png
    except:
        widget['text'] = "Lenna.gif"
        widget['image'] = widget.lenna_image_gif
    widget.pack()
    root.mainloop()
Answered By: Nae

Just convert the jpg format image into png format. It will work 100%.

Answered By: Kazi

This code works for me, also you should consider if you have any other button or labels in that window and you not use .place() it will not work properly.

from Tkinter import*
from PIL import Image, ImageTk

img  = Image.open("path/x.png") 
photo=ImageTk.PhotoImage(img)
lab=Label(image=photo).place(x=50,y=50)
Answered By: beginner

I do like this and it’s work :

def openImage(name):
    img = ImageTk.PhotoImage(Image.open(name))
    Label(fen, image=img).place(x=100,y=100).pack()

and you call the function like this:

openImage("yourImagePath")
Answered By: Remakerz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.