rectangletangle
rectangletangle

Reputation: 53051

Manipulating individual pixel colors in the Tkinter Canvas widget

I'd like to be able to change the color of indvidual pixels within the Tkinter canvas widget.

My first attempt at this, I created many line objects and made them act as pixels.

self.Ca is my Canvas widget

snippet:

i0 = 0
while i0 < height:
    i1 = 0
    while i1 < width:
        pix = self.Ca.create_line(i1, i0, i1+1, i0, fill=self.Hex, width=1)
        self.matrix.append(pix)
        i1 = i1 + 1
    i0 = i0 + 1

Although this works, it devours resources. After the widget gets much bigger than 100X100 performance drops off quickly.

Is there a better way?

Upvotes: 1

Views: 2942

Answers (2)

No Braves Studio
No Braves Studio

Reputation: 171

You can also work with rectangles... to draw a rectangle on the pixel x,y

canvas.create_rectangle(x,y,x+1,y+1,fill="red")

But of course it is not the most effective...

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386352

Instead of using the canvas, start with a blank image of the appropriate size. You can manipulate individual pixels in the image pretty easily, though you lose the ability to draw objects such as lines, circles, etc.

Bottom line is that the canvas isn't designed for dealing with individual pixels.

Upvotes: 1

Related Questions