【发布时间】:2018-07-17 13:48:10
【问题描述】:
尝试了不同的可能性,终于要发帖了:
此程序将向用户显示图像。用户将使用鼠标单击来单击图像的不同区域。每次鼠标点击时,点都会被收集到 list_of_points 列表中。在鼠标右键单击时,我想从 list_of_points 列表中生成一个多边形。多边形库必须是
PIL.ImageDraw.Draw.polygon(xy, fill=None, outline=None)
我反复收到以下错误:
TypeError: 函数只需要 2 个参数(给定 1 个)
这个错误:
self.draw.draw_polygon(xy, ink, 0) SystemError: new style getargs format but argument is not a tuple
代码如下:
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()
`
【问题讨论】:
-
你做了一个
print (poly),我们可以得到这个变量的内容吗? -
zip(list_of_points) 那么 poly 中的值是:[((411, 113),), ((158, 169),), ((344, 364),)]
-
poly=tuple(list_of_points) poly 中的值是:((432, 224), (196, 245), (268, 379)) 没有错误,但我没有绘制多边形。
-
@john 多边形是在 PIL 图像上绘制的,而不是在画布上显示的
PhotoImage上,这就是我们看不到它的原因。 -
如何在 PhotoImage 上为我处理画布和 PIL 的这个特定程序获取它?
标签: python tkinter python-imaging-library