2

I need to load and display possibly thousands of images, but currently Image.open() and ImageTk.PhotoImage() take way too long.

Is there any way to speed up this process, or an alternative method to use?

Lachlan
  • 360
  • 5
  • 14
Ciaran
  • 21
  • 3
  • Why not load the images as needed? I doubt that you need all 1000 images when the program has just started up. If you really want to, you can open the images in other threads but you must call `ImageTk.PhotoImage` from the thread where you created the `tk.Tk` – TheLizzard Sep 17 '22 at 11:00
  • Yeah, so my project is to do with image pre processing, so I need to have the images loaded, so the user can select the ones that they want. It needs to be interactive, so that's why I'm loading them :/ – Ciaran Sep 17 '22 at 11:03
  • How are you displaying 1000s of images on the screen at the same time? Why not make a background loop that slowly loads all of the images that aren't going to be displayed right away. – TheLizzard Sep 17 '22 at 11:06
  • I'm using a canvas with a scrollbar. That's a good idea actually, one uses the after() method to do stuff in the backround right? – Ciaran Sep 17 '22 at 11:08
  • Yes, a background `.after` loop that slowly loads the images that the user isn't going to be able to see right away is the best option. That is how some website work (apparently). – TheLizzard Sep 17 '22 at 11:10
  • @TheLizzard Just got a tentative solution working with .after, thanks so much! My first attempts were confusing me until I realised I wasn't calling a lambda:myMethod(args), so it wasn't waiting at all, and just executing the method – Ciaran Sep 17 '22 at 12:59
  • This is how it works: `.after(delay in milliseconds, function, *args)`, and yes - you have to be careful to not make [this](https://stackoverflow.com/q/5767228/11106801) mistake. – TheLizzard Sep 17 '22 at 13:07
  • That was the exact mistake I made! Felt very silly – Ciaran Sep 17 '22 at 17:54

0 Answers0