1

I'm using Python Image and Tkinder. I am creating a blank image and I would like every click on it a pixel to turn red, but the image does not refresh. What is the best way to do this?

import tkinter as tk
from PIL import Image, ImageTk

# PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]
img = Image.new( 'RGB', (500,500), "white") # create a new white image
pixels = img.load() # create the pixel map


window = tk.Tk()
canvas = tk.Canvas(window, width=img.size[0], height=img.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(img)
canvas.create_image(img.size[0]//2, img.size[1]//2, image=image_tk)

def mouseClick( event):

    pixels[event.x, event.y]= (255,0,0) #print pixel red

canvas.bind("<Button-1>", mouseClick)

window.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Here you are just modifying the variable `pixels`. My guess would be that you now need to reassign the edited image onto the canvas. Something like this: https://stackoverflow.com/questions/19838972/how-to-update-an-image-on-a-canvas . Its not like a pointer. The links shows how you can edit an already created canvas image. – Sens4 Dec 19 '18 at 16:19

1 Answers1

0

I don't know anything about tkinter but this seems to do what you want - it may be horrible but maybe it will suffice until someone comes along who knows better...

#!/usr/bin/env python3

import tkinter as tk
from PIL import Image, ImageTk

# PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]
img = Image.new( 'RGB', (500,500), "white") # create a new white image

window = tk.Tk()
canvas = tk.Canvas(window, width=img.size[0], height=img.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(img)
canvas.create_image(img.size[0]//2, img.size[1]//2, image=image_tk)

def mouseClick( event):
    x, y = event.x, event.y
    print("x: {}, y: {}".format(x,y))
    # Update image
    img.putpixel((x, y),(255,0,0))
    # Update screen
    canvas.create_oval(x, y, x, y, width = 0, fill = 'red')

canvas.bind("<Button-1>", mouseClick)

window.mainloop()
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • This helped me. https://stackoverflow.com/questions/11220487/how-do-i-update-images-on-a-tkinter-canvas canvas.itemconfigure(myimg, image=picture)# Thank you – Mateus Guilherme Dec 19 '18 at 18:51
  • If you have got code now that works, please put it as your answer and accept it as correct then you will get the points and other people looking here in future will know how to do it. – Mark Setchell Dec 19 '18 at 19:57
  • Why start an answer to a question about tkinter with "I don't know anything about tkinter" – Greg Harley Nov 29 '19 at 23:36
  • 2
    @GregHarley ... because what I suggested may be poor practice, and I was unsure if it was sensible, so I warned the OP. As it turns out, no one else helped and the OP was presumably happy enough with it to accept it. I have no idea why anyone else, who clearly has no better suggestion would down-vote it, but there you go. Personally I’m glad to have helped. – Mark Setchell Nov 29 '19 at 23:43