Python Forum

Full Version: Gif with PhotoImage
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

1. Question:
I am learning Python in Python2 book (didn't find a better python3 book) a long example (which i reduced to fow lines), i has been tried to import a gif picture using the class PhotoImage... but i always get the following error :

from tkinter import *
root = Tk()
path = 'C:\\Users\\Desktop\\Mix\\Big pics\\wolf.gif'
bild = PhotoImage(file=path)

Traceback (most recent call last): File "C:\Users\mlakhili\Desktop\Python\Licht.pyw", line 4, in <module> bild = PhotoImage(file=path) File "C:\Python27\lib\lib-tk\", line 3371, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python27\lib\lib-tk\", line 3325, in __init__'image', 'create', imgtype, name,) + options) TclError: couldn't recognize data in image file "C:\Users\mlakhili\Desktop\Mix\Big pics\wolf.gif"
i have read in Forums that others had similar problems because they tried to import JPG or so..but i am trying with gif format, so what am i doing wrong

2. Question:
this works in Python2. How do i do it in Python3 ?

import tkFileDialog
path = tkFileDialog.askopenfilename()
This should work. It's important to have a reference to the PhotoImage object,
otherwise it's garbage collected and will not displayed. With PIL you can open
all kind of bitmaps.

To install PIL on Python 3:
pip install pillow
from tkinter import Tk, Button, Label
from tkinter.filedialog import askopenfilename
from PIL import Image
from PIL.ImageTk import PhotoImage 

class App:
    def __init__(self):
        self.root = Tk()
        self.image = None
    def setup_gui(self):
        root = self.root
        self.label = Label(root)
        Button(root, text='Open', command=self.open_pic).pack()
        Button(root, text='Exit', command=self.root.destroy).pack()

    def open_pic(self):
        file = askopenfilename()
        if not file:
        img =, 320))
        self.image = PhotoImage(img)
        # keeping image as reference, otherwise
        # it's garbage collected
        self.label['image'] = self.image
        # assign the image to the label
    def start(self):

app = App()
I tried everything, watched hundred of Videos to install pip pillow or whatever black says successfully installed...
and still i get errors like :
No module named 'PIL'
Managed to install it.
and everything is working. Thanks a lot DeaD_EyE :)