Python Forum
Gif with PhotoImage - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Gif with PhotoImage (/thread-19992.html)



Gif with PhotoImage - Friend - Jul-23-2019

Hi all,

1. Question:
I am learning Python in Python2 book (didn't find a better python3 book)...in 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)
root.mainloop()


Error:
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\Tkinter.py", line 3371, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 3325, in __init__ self.tk.call(('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()



RE: Gif with PhotoImage - DeaD_EyE - Jul-23-2019

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.root.title('PhotoImage')
        self.image = None
        self.setup_gui()
    
    def setup_gui(self):
        root = self.root
        self.label = Label(root)
        self.label.pack()
        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:
            return
        img = Image.open(file).resize((480, 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):
        self.root.mainloop()


app = App()
app.start()



RE: Gif with PhotoImage - Friend - Jul-23-2019

I tried everything, watched hundred of Videos to install pip pillow or whatever black magic...it says successfully installed...
and still i get errors like :
Error:
No module named 'PIL'



RE: Gif with PhotoImage - Friend - Jul-23-2019

Managed to install it.
and everything is working. Thanks a lot DeaD_EyE :)