[Python] Is there a way to speed up python's Tkinter/Pillow Tkimage.Photoimage?

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently trying to make fancy widgets for Tkinter and for this i made an image collection of 40x50 images (240x240 pixels, RGBA). When i try to load all these images (they are in a pickle file, already containing pillow images), it takes way over a minute for this simple loop to execute, where ImageTk.Photoimage seems to be the bottleneck:

    imagearray = pickle.load(open(self.GAUGE_UI_FILENAME, "rb")) #40x50 array of 240x240 pixel RGBA images
    converted_imagelist = []
    for imagelist in imagearray:
    for img in imagelist:
    converted_imagelist.append(ImageTk.PhotoImage(img, size = (240,240)))
    self.gauge_images.append(converted_imagelist)
    converted_imagelist = []


    I already tried threading it, but it seems like ImageTk.PhotoImage calls functions of Tkinter which only are allowed to run in the main thread. Am i doing something wrong? Can i somehow prepare the images for tkinter to reduce the startup time? The unpickling of the rather large nested list happens in a split second, but the conversion for tkinter takes ages. After the images are loaded i can cycle through them fine in a reasonable amount of frames per second to display the animations but the execution time of this few lines of code makes me go grab a Tea before the application actually shows the GUI.

    Continue reading...

Compartilhe esta Página