Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import Image, ImageTk, ImageDraw
- import numpy as np
- coord=[] # for saving coord of each click position
- Dict_Polygon={} # Dictionary for saving polygon
- list_of_points=[]
- flag=True
- label=0
- # Input image
- img = Image.open("test.jpg")
- draw = ImageDraw.Draw(img)
- def draw_lines(event):
- mouse_xy = (event.x, event.y)
- func_Draw_lines(mouse_xy)
- def func_Draw_lines(mouse_xy):
- center_x, center_y = mouse_xy
- if canvas.old_coords:
- x1, y1 = canvas.old_coords
- canvas.create_line(center_x, center_y, x1, y1)
- # add clicked positions to list
- if flag==True:
- list_of_points.append(mouse_xy)
- canvas.old_coords = center_x, center_y
- def draw_poly(event):
- numberofPoint=len(list_of_points)
- if numberofPoint>2:
- #draw =ImageDraw.Draw(img)
- poly=zip(list_of_points)
- print(poly)
- draw.polygon(poly, fill=None, outline=(255, 0, 0))
- # label= canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
- canvas.old_coords=None
- list_of_points[:]=[]
- # Main function
- if __name__ == '__main__':
- root = Tk()
- # 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()
- canvas.old_coords = None
- # bind function to canvas to generate event
- canvas.bind("<Button 3>", draw_lines)
- canvas.bind("<Button 1>", draw_poly)
- root.mainloop()
- `
Add Comment
Please, Sign In to add comment