Python Forum
Gif with PhotoImage
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gif with PhotoImage
#1
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()
Reply
#2
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()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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'
Reply
#4
Managed to install it.
and everything is working. Thanks a lot DeaD_EyE :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question on tkinter canvas PhotoImage gr3yali3n 1 2,105 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Get the parent label of a ImageTk.PhotoImage delphinis 1 2,364 Aug-02-2020, 03:21 PM
Last Post: deanhystad
  Move PhotoImage dan789 2 2,661 Dec-19-2018, 06:00 PM
Last Post: dan789
  [Tkinter] PhotoImage and Jpeg images. rozen 3 4,534 Jan-07-2018, 08:34 AM
Last Post: Gribouillis
  [Tkinter] createing a tkinter photoimage from array in python3 pootle 2 16,716 Oct-18-2016, 09:28 AM
Last Post: pootle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020