0

The task is to create multiple polygons on image by mouse clicks on different positions on image. I used Python tkinter for this task. I displayed the image to user. I got the x,y co-ordinates when user clicks on image. How to create polygon from different co-ordinates on which the user clicked. Initially, I want to create simple line on mouse click but as the number of selected points increases, it should create polygon. This is the part of code I did for this task.

# Function to get the co-ordianates of  mouse clicked position and draw polygons
def draw_plygons(event):
    mouse_xy = (event.x, event.y)

# Draw canvas for iput image to pop up image for clicks
    filename = ImageTk.PhotoImage(img)
    canvas = Canvas(root,height=img.size[0],width=img.size[0])
    canvas.image = filename
    canvas.create_image(0,0,anchor='nw',image=filename)
    canvas.pack()
# bind function to canvas to generate event
    canvas.bind("<Button 3>", draw_polygons)
    root.mainloop()
john
  • 101
  • 1
  • 14

1 Answers1

0

Here is the solution, How I did it.

def draw_polygons(event):
        mouse_xy = (event.x, event.y)

I collected all the clicked points in list_of_points list and then drawn using:

canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
john
  • 101
  • 1
  • 14