Skip to content
Advertisement

How to save what a tkinter window looks like without screenshot? (Possibly a numpy array?)

I have a short program that generates images of 1, 2, 3, and 4 random coloured circles to train a neural network on. My issue is, just doing 4000 images takes about 20-30 mins, and I need about 50000 images. My current method is to create the image, screenshot it, and then delete the tkinter object and restart. The issue is, windows 11 has a little fade in/slide animation when a new window is created, only about 200 ms, but it adds up quite a bit, because I need to wait for the animation to finish to take the screenshot.

So my question is, Is there another way to save a tkinter canvas other than screenshot?

I want to point out that I am putting these images into a numpy array, so putting it directly into an array is an option, but I need some way to save that as a file, so I dont need to generate the images every time.

My current code (only showing how I make 4 circles)

JavaScript

Advertisement

Answer

I think your approach of destroying and creating a new window over each iteration is tedious way of doing it. Instead, you can clear the canvas each time and keep creating this random circles and then click a picture of it.

JavaScript

I replaced your ss with my capture which is taken from another answer of mine, the logic is same but adds more flexibility, you can use your function with necessary changes. I suggest you first run this with a LIMIT = 10 and check if the delay are okay, if not you can adjust it and then move on to produce your 50000 image-set.

If you furthermore want to convert/load your image as a numpy array, then check the first reference link below.

Extra references:

Advertisement