How do I get specific pixels of the tkinter canvas?

Question:

I have an issue with the Tkinter Canvas, saving it to an image, but more importantly, getting a specific pixel of the Tkinter Canvas. All I need is for that specific X, Y coordinate, I want its color value. By ‘color value’, any value I can use to draw a pixel with the python imaging library would work.

Here’s what I tried to do to make it work:

  1. I first tried to use python imaging library’s image grabbing class to get the Canvas Image, but that didn’t work as expected.
  2. I then tried to convert the Tkinter Canvas to an image by with the “eps” file, but that didn’t work, and GhostScript didn’t work either.
  3. Next, I searched to see if there was any way to get the pixel at a certain coordinate on a Tkinter canvas to no avail. That’s where I posted this.

Next, here’s the code I tried for step 2:

    screen = w
    @staticmethod
    def image():
        w.postscript(file=".canvas_script.eps")
        img = Image.open(".canvas_script.eps")
        return img
        # GhostScript Error

Expected Result: My expected result is to get the color of any certain point, or at least to get a color of the point that I can convert to another color tuple or value. Then, to put draw that on a python imaging library image.

Actual Result: I found no method on the internet when I researched that solved my question, and no method in the documentation.

If anyone can help me get the color from a specific coordinate of the Tkinter Canvas, say (0, 0), please do. Thanks!

Asked By: Corman

||

Answers:

How do I get specific pixels of the tkinter canvas?

You can’t. The canvas isn’t a pixel oriented widget. The best you can do is use some other library to create a screenshot of the canvas, and then use the image to give you the values that you want.

Answered By: Bryan Oakley

I’m trying to somewhat the same thing.
I create a coarse (40 by 25) set of rectangles to create a "Braille" version of the canvas to help the blind "visualize" the picture. I want to support magnification by taking a subset of the rectangles and
expand it so I can create a coarse (40 by 25) set of rectangles rendering the smaller subset. I’m thinking of using the canvas.find_overlapping() to find canvas items which overlap the smaller contained regions therefore the color of each contained smaller rectangular regions. I haven’t done it yet but I’ll shall try.

Answered By: crs