Python Forum
[Tkinter] Loading Images to Tkinter with Pillow
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Loading Images to Tkinter with Pillow
#1
Hi everyone!

I'm trying as an experiment to build a very basic image viewer program with tkinter:

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

index = 0
image_list = glob("Img\\*PNG")
image_obj = []

for x in image_list:
    image_obj.append(ImageTk.PhotoImage(Image.open(x)))


def foward():
    global index
    if (index < len(image_obj)):
        index = index + 1
        visualizer.config(image=image_obj[index])
    else:
        pass


def backward():
    global index
    if (index > 0):
        index = index - 1
        visualizer.config(image=image_obj[index])
    else:
        pass


root = Tk()
visualizer = Label(root, image=image_obj[0])
visualizer.grid(row=0, column=0, columnspan=3)

back_button = Button(root, text='<<', command=foward)
quit_button = Button(root, text= 'EXIT', command=root.destroy())
foward_button = Button(root, text='>>', command=backward)

root.mainloop()
And I'm running into the following problem:

Error:
Traceback (most recent call last): File ".\photo_visualizer.py", line 10, in <module> image_obj.append(ImageTk.PhotoImage(Image.open(x))) File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageTk.py", line 112, in __init__ self.__photo = tkinter.PhotoImage(**kw) File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3994, in __init__ raise RuntimeError('Too early to create image') RuntimeError: Too early to create image Exception ignored in: <function PhotoImage.__del__ at 0x03F816A0> Traceback (most recent call last): File "C:\Users\tomi_\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageTk.py", line 118, in __del__ name = self.__photo.name AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
I tried a lot of thing to make the PIL.PhotoImage metod work for me but nothing worked. Hope someone can guide me about this (or tell me about any other way to upload images to Tkinter, this was the only one that I found)
Reply
#2
This program fails too:
from tkinter import *

image = PhotoImage(file='image.png')
The error is
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 3, in <module> image = PhotoImage(file='image.png') File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 4061, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 3994, in __init__ raise RuntimeError('Too early to create image') RuntimeError: Too early to create image
This program runs
from tkinter import *

Tk()
image = PhotoImage(file='image.png')
Reply
#3
(Jun-18-2020, 03:03 AM)deanhystad Wrote: This program fails too:
from tkinter import *

image = PhotoImage(file='image.png')
The error is
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 3, in <module> image = PhotoImage(file='image.png') File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 4061, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 3994, in __init__ raise RuntimeError('Too early to create image') RuntimeError: Too early to create image
This program runs
from tkinter import *

Tk()
image = PhotoImage(file='image.png')

deanhystad THANK YOU!
You're totally right, to use the PhotoImage method I have to call it inside the Tk() loop and now that I did that my code is working!
Again thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems trying to position images with Tkinter emont 3 772 Dec-12-2023, 07:20 AM
Last Post: menator01
  Images in tkinter menator01 0 1,602 Apr-25-2020, 12:49 AM
Last Post: menator01
  tkInter and Pillow don't display any images in GUIs - program gives errors instead SomeRandomGuy 9 10,818 Oct-29-2019, 02:57 PM
Last Post: SomeRandomGuy
  [Tkinter] Help with tkinter; images and button commands SheeppOSU 2 2,994 Mar-28-2019, 02:01 AM
Last Post: woooee
  Remove duplicate images - tkinter? darter 5 5,360 Nov-10-2018, 10:54 PM
Last Post: Larz60+
  Tkinter widgets not fully loading nortski 3 3,543 Apr-02-2018, 12:23 PM
Last Post: nortski

Forum Jump:

User Panel Messages

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