43

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()
Nae
  • 14,209
  • 7
  • 52
  • 79
Damien
  • 563
  • 2
  • 6
  • 10
  • 1
    I recommend you read the book "Python and TKinter programming". Very good book, thorough. You can probably find them on eBay for lower prices. That's assuming you really want to use TKinter. I do recommend Qt instead of Tkinter though – frankliuao Jul 02 '17 at 04:34

10 Answers10

40

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()
josav09
  • 401
  • 1
  • 4
  • 4
  • 3
    Linux users note: Not only is tkinter typically separated into a separate package that must be installed — but imagetk can be too. So on Debian/Ubuntu you install them with   `sudo apt install python3-tk python3-pil python3-pil.imagetk`   (python3-pil *is* the pillow fork). – Dave Rove Mar 22 '20 at 13:19
19

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.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
14

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

MarAvFe
  • 468
  • 4
  • 15
Walid Bousseta
  • 1,329
  • 2
  • 18
  • 33
8

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()
    
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Takahiro
  • 1,222
  • 10
  • 11
6

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()
Nae
  • 14,209
  • 7
  • 52
  • 79
5

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!

incalite
  • 3,077
  • 3
  • 26
  • 32
  • 1
    `script.py` will only need to be in the same folder as the image if the image path is relative. – Corman May 29 '20 at 18:27
  • 1
    Dead link, effbot.org is no longer online. To download PIL use the steps outlined in the [official docs](https://pillow.readthedocs.io/en/latest/installation.html) – TheLizzard Dec 31 '22 at 19:51
3

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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
beginner
  • 85
  • 10
1

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.

devonj
  • 1,198
  • 1
  • 13
  • 24
1

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

jkdev
  • 11,360
  • 15
  • 54
  • 77
Kazi
  • 21
  • 1
1

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")
Remakerz
  • 11
  • 4