0

The first answer in this link is what I am regarding in the following: python tkinter: how to work with pixels?

Regarding tkinter's Photoimage class, how would I clear it and update it so that I can see the frames live and make animations?

img.update() or image.clear() would be something that I am looking for, but those aren't apart of the class.

1 Answers1

1

Clearing the image

The method blank will erase all of the data in the image. From the canonical documentation:

Blank the image; that is, set the entire image to have no data, so it will be displayed as transparent, and the background of whatever window it is displayed in will show through.

Updating the image

To update it, you can use the put method to put a color in one or more pixels, or the copy method to copy a portion of one image on top of another.

For example, to put a red pixel at 100,100 you could do this:

the_image.put("red", to=(100,100))

The image will be updated immediately. Putting pixels one at a time is very slow, partly due to the immediate update. It's usually best if you can set whole rows or whole columns at once. You can do that by passing in a tuple of tuples representing rows and columns.

For example, the following will create alternating rows of red and white with a single update, with each row being five pixels wide:

    data = (
        ("red",   "red",   "red",   "red",   "red"),
        ("white", "white", "white", "white", "white"),
        ("red",   "red",   "red",   "red",   "red"),
        ("white", "white", "white", "white", "white"),
        ("red",   "red",   "red",   "red",   "red"),
        ("white", "white", "white", "white", "white"),
    )
    the_image.put(data, to=(0,0))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I have a follow up question - the img class updates aren't live. I'm trying to make an animation. Is there an `img.update()` equivalent? When I run my animation it's static until the program is done running and shows the final frame. ------- This answer was helpful regarding the clearing but I can't see the actual process taking place. I can tell it worked though, because I used to see the trails of the moving pixels and don't now. – Corpus Shmorpus Feb 26 '18 at 21:22
  • 1
    @MegaFirm: you need to create a new question with a [mcve]. Generally speaking, when you update an image it is updated on the screen immediately. – Bryan Oakley Feb 26 '18 at 21:37
  • This answer doesn't answer the whole question. That's all I was trying to say. – Corpus Shmorpus Feb 26 '18 at 22:20
  • @MegaFirm: I apologize. I've added information on how to update the image. – Bryan Oakley Feb 26 '18 at 22:43
  • No Problems, I've got to work on being more straightforward. Also, It works like a charm – Corpus Shmorpus Feb 28 '18 at 01:40
  • Also, is there a faster way to do this? I guess this is somewhat related to the question, because this whole thing is taking a while to render. also, I eventually want to make it a texture so I don't want to use `canvas.polygon`https://gist.github.com/SnugBug/6702d16064a3d8d730a1c25c1f8daedd – Corpus Shmorpus Feb 28 '18 at 02:32
  • @MegaFirm: the best you can probably do to speed it up is do as few calls to `put` as possible. – Bryan Oakley Feb 28 '18 at 02:57
  • https://gist.github.com/SnugBug/058421ba488356c8871dd8d14fbe9e2f Still extremely slow. It's imperative that the cause of the slowness is in the 700x700 tuple, but I don't want to make a tuple that tracks to the 3D objects 2D projected coordinates and bounds the corners, that's just too much work. Plus, it would likely speed it up only a bit. I don't know what to do from here, this is supposedly the fastest tkinter gets with individual pixels. I have limited it to one call to put per frame. – Corpus Shmorpus Mar 01 '18 at 23:09