Python Forum

Full Version: Loading Images to Tkinter with Pillow
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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')
(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!