简体   繁体   中英

Tkinter:change a pixel's color

I load an image in a TkInter canvas to allow the user to draw a point by a mouse right lick button. After saving the coordinates of the pixel where the mouse button was pressed, I change the pixel in question to red color using OpenCV.

No error occurs, however there is no effect on the picture. I mean, I do not have the expected result.

may be someone could tell me what's wrong ?

import PIL.Image
import Image
import ImageTk
from Tkinter import *    
import numpy as np
import cv2    

class ExampleApp(Frame):
   def __init__(self,master):
        Frame.__init__(self,master=None)
        self.x = self.y = 0
        self.imcv=None
        self.canvas=None   

   def dessiner(self):
       # Load the imge and allow user to scroll it if it is large.
       self.canvas=Canvas(self,cursor="cross",width=600,height=600)
       self.canvas.bind("<ButtonPress-1>",self.on_button_press)
       self.sbarv=Scrollbar(self,orient=VERTICAL)
       self.sbarh=Scrollbar(self,orient=HORIZONTAL)
       self.sbarv.config(command=self.canvas.yview)
       self.sbarh.config(command=self.canvas.xview)

       self.canvas.config(yscrollcommand=self.sbarv.set)
       self.canvas.config(xscrollcommand=self.sbarh.set)

       self.canvas.grid(row=0,column=0,sticky=N+S+E+W)
       self.sbarv.grid(row=0,column=1,stick=N+S)
       self.sbarh.grid(row=1,column=0,sticky=E+W)
       self.im = PIL.Image.open("image.jpg")
       self.widt,self.heigt=self.im.size
       self.canvas.config(scrollregion=(0,0,self.widt,self.heigt))
       self.tk_im = ImageTk.PhotoImage(self.im)
       self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)   


   def on_button_press(self,event):
       self.lecanvas=event.widget
       self.x=self.lecanvas.canvasx(event.x)
       self.y=self.lecanvas.canvasy(event.y)
       self.canvas.create_oval(self.x,self.y, self.x+1,self.y+1,outline='red')
       print self.x,self.y


   def resumer(self):
       self.imcv=cv2.imread("image.jpg")
       self.imcv[self.x,self.y,0]=0
       self.imcv[self.x,self.y,1]=0
       self.imcv[self.x,self.y,2]=255
       cv2.imwrite("result.jpg",self.imcv)
       cv2.namedWindow("gl",cv2.WINDOW_NORMAL)
       cv2.imshow("gl",self.imcv)
       cv2.waitKey(0)
       cv2.destroyAllWindows()






if __name__ == "__main__":
    root=Tk()
    app = ExampleApp(root)
    app.dessiner()
    app.resumer()    
    app.pack()
    root.mainloop()

Note that I also get the cv2 window displayed before the Tkinter window is displayed.

EDIT:

Following the comments of the users below, I guess I can get the coordinates of that pixel I draw on TkInter interface using a red color. So I want to recolor that pixel into red using OpenCV but I guess it does not take effect using OpenCV. Note that I must do this in OpenCV for further image processing:

def resumer(self):
       print"inside Resumer function:"
       print"Resume: ({},{})".format(self.x,self.y)
       self.imcv=cv2.imread("image.jpg")
       self.imcv[self.x,self.y,0]=0
       self.imcv[self.x,self.y,1]=0
       self.imcv[self.x,self.y,2]=255
       cv2.imwrite("result.jpg",self.imcv)
       cv2.namedWindow("gl",cv2.WINDOW_NORMAL)
       cv2.imshow("gl",self.imcv)
       cv2.waitKey(0)
       cv2.destroyAllWindows()

The order of calling the is this one now:

if __name__ == "__main__":
    root=Tk()
    app = ExampleApp(root)
    app.pack()
    app.dessiner()
    root.mainloop()
    app.afficher_pixel()
    app.resumer() 

Any help is appreciated Thank you in advance.

您需要更改映像的内存副本( self.tk_im )。

You mention two problems, one about changing the image and another about the cv2 window displayed before the Tkinter window.

The second item is easy, so I'll address it first. It's caused by the fact that you call root.mainloop() after the call to app.resumer() (which displays the cv2 window). You can fix either by executing app.resumer() in response to some event — say from clicking a new button you add — or more simply by moving the call to it to after root.mainloop() returns (which happens when the Tkinter window is closed by default).

The other problem about changing the pixel to red in the OpenCV window may have something to do with the self.x and self.y image coordinates you got from the Tkinter event being incorrect somehow, so I would check those and make sure you're getting the proper values and using them correctly — for example do you have them in the right order?

Try using canvas.update() after you change it. It could be that the code works but just doesn't show the changes on the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM