0

I have this code where the user can paint using his mouse:

from Tkinter import *

class Test:
   def __init__(self):
       self.b1="up"
       self.xold=None
       self.yold=None
   def test(self,obj):
       self.drawingArea=Canvas(obj)
       self.drawingArea.pack()
       self.drawingArea.bind("<Motion>",self.motion)
       self.drawingArea.bind("<ButtonPress-1>",self.b1down)
       self.drawingArea.bind("<ButtonRelease-1>",self.b1up)
   def b1down(self,event):
       self.b1="down"
   def b1up(self,event):
       self.b1="up"
       self.xold=None
       self.yold=None
   def motion(self,event):
      if self.b1=="down":
           if self.xold is not None and self.yold is not None:
               event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=4,smooth=TRUE)
           self.xold=event.x
           self.yold=event.y


if __name__=="__main__":
   root=Tk()
   root.wm_title("Test")
   v=Test()
   v.test(root)
   root.mainloop()

I wonder how to save the coordinates of the drawn line knowing that the thickness of the line is 4 (the width can be any integer number less than 10) ?

Without the thickness option, the answer is evident for me.

Thank you in advance.

  • What exactly do you mean by "coordinates of the drawn line"? Do you mean you want the coordinates of all pixels that are colored? Showing the "evident" answer without the thickness option may also help. – fhdrsdg May 06 '15 at 10:21
  • @fhdrsdg now I am struggling to get the coordinates of the poins only if the thickness is 1 (by default) :( not as evident as I thought –  May 06 '15 at 11:48
  • That's what I thought, and that's why I asked. What do you want to do with the coordinates though? If for instance you want to draw the same lines on an image you could try the [ImageDraw module](http://effbot.org/imagingbook/imagedraw.htm#tag-ImageDraw.Draw.line) in PIL. If you want them for any other reason there also might be a better way to do this than trying to find all coordinates. – fhdrsdg May 06 '15 at 11:58
  • @fhdrsdg I need those coordinates because behind my TkInter user interface, I process an image in OpenCV. So when the user draws with the mouse over the image, I needto get those coordinates of all the points he drew in order to give them to OpenCV for special image processing. I asked my question in this simple form because my real problem is much more larger. –  May 06 '15 at 12:10
  • OpenCV also has a [line drawing function](http://docs.opencv.org/modules/core/doc/drawing_functions.html#line), you can try mimicking every line draw you do on the canvas in OpenCV too. I'm not sure if that will give the exact same results, but it'll probably be better than trying to find all pixel coordinates. – fhdrsdg May 06 '15 at 12:14
  • @fhdrsdg yes, that is exactly what I want. But have you a conceptional idea on how to draw the same line on OpenCV while or after the the user draws the line ona Tkinter canvas ? –  May 06 '15 at 12:19
  • 1
    I don't have any experience with OpenCV, but you could probably just after every (only 1 in this example) call to `event.widget.create_line` place a call to `cv.Line`, using the same coordinates, color and thickness. If you can't figure it out, create a new question. – fhdrsdg May 06 '15 at 12:26
  • @fhdrsdg I think it is feasible what you said here. I will try it. Thank you very much. –  May 06 '15 at 12:29

1 Answers1

1

You cannot get the information you want, if what you want is a list of all the pixels that get changed when you draw a wide line. The only information you get when creating a line on the canvas are the coordinates of the endpoints.

If the line is completely horizontal or vertical you can get the bounding box of the line, but that won't work for diagonal lines.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ok, thank you. Is this answer valid even when thickness is 1 (by default) ? –  May 06 '15 at 11:35
  • @Nakkini: yes. No matter the thickness, all you can inquire about are the endpoints, or the bounding box if the line is horizontal or vertical. – Bryan Oakley May 06 '15 at 11:55