Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:函数只接受2个参数(给定1个)_Python_Tkinter_Python Imaging Library - Fatal编程技术网

Python TypeError:函数只接受2个参数(给定1个)

Python TypeError:函数只接受2个参数(给定1个),python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,在尝试不同的可能性之后,最后要发布: 此程序将向用户显示图像。用户将使用鼠标单击图像的不同区域。每次单击鼠标时,都会在点列表中收集点。单击鼠标右键,我想从点列表生成多边形。多边形的库必须是 PIL.ImageDraw.Draw.polygon(xy,填充=无,轮廓=无) 我反复遇到以下错误: TypeError:函数只接受2个参数(给定1个) 这个错误是: self.draw.draw_多边形(xy,墨水,0) SystemError:新样式getargs格式,但参数不是元组 代码如下: fro

在尝试不同的可能性之后,最后要发布:

此程序将向用户显示图像。用户将使用鼠标单击图像的不同区域。每次单击鼠标时,都会在点列表中收集点。单击鼠标右键,我想从点列表生成多边形。多边形的库必须是

PIL.ImageDraw.Draw.polygon(xy,填充=无,轮廓=无)

我反复遇到以下错误:

TypeError:函数只接受2个参数(给定1个)

这个错误是:

self.draw.draw_多边形(xy,墨水,0) SystemError:新样式getargs格式,但参数不是元组

代码如下:

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()

`
从Tkinter导入*
导入图像、ImageTk、ImageDraw
将numpy作为np导入
坐标=[]#用于保存每个单击位置的坐标
Dict_Polygon={}#用于保存多边形的字典
点列表=[]
flag=True
标签=0
#输入图像
img=Image.open(“test.jpg”)
draw=ImageDraw.draw(img)
def绘制线(事件):
鼠标_xy=(event.x,event.y)
函数绘制线(鼠标xy)
定义函数绘制线(鼠标xy):
中心x,中心y=鼠标x
如果canvas.old_坐标:
x1,y1=画布。旧坐标
画布。创建线(中心x,中心y,x1,y1)
#将单击的位置添加到列表中
如果flag==True:
列出\u点。追加(鼠标\u xy)
canvas.old_coords=center_x,center_y
def draw_poly(事件):
numberofPoint=len(点列表)
如果numberofPoint>2:
#draw=ImageDraw.draw(img)
poly=zip(点列表)
打印(多边形)
多边形(多边形,填充=无,轮廓=(255,0,0))
#标签=画布。创建多边形(点列表,填充='',轮廓='',绿色,宽度=2)
canvas.old_coords=无
点列表[:]=[]
#主要功能
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
#为iput图像绘制画布,以弹出图像进行单击
filename=ImageTk.PhotoImage(img)
画布=画布(根,高度=img.size[0],宽度=img.size[0])
canvas.image=文件名
canvas.create_image(0,0,anchor='nw',image=filename)
canvas.pack()
canvas.old_coords=无
#将函数绑定到画布以生成事件
canvas.bind(“,绘制线)
canvas.bind(“,draw\u poly)
root.mainloop()
`

您需要一种类似-

[(411, 113), (158, 169), (344, 364)]
但你传递的是元组中的元组-

((411, 113),), ((158, 169),), ((344, 364),)]
试试这个结构-

[(432, 224), (196, 245), (268, 379)]
或者直接列出也可以——

[432, 224, 196, 245, 268, 379]
查看文档


当函数需要两个参数时,Tuple of Tuple成为函数的一个参数。您不需要
zip(poly)
,只需传递
numberofPoint
就可以了。如果有帮助,请告诉我您需要一种类似-

[(411, 113), (158, 169), (344, 364)]
但你传递的是元组中的元组-

((411, 113),), ((158, 169),), ((344, 364),)]
试试这个结构-

[(432, 224), (196, 245), (268, 379)]
或者直接列出也可以——

[432, 224, 196, 245, 268, 379]
查看文档


当函数需要两个参数时,Tuple of Tuple成为函数的一个参数。您不需要
zip(poly)
,只需传递
numberofPoint
就可以了。让我知道当我直接将
列表点
传递到
draw.polygon
时,这是否有帮助,它工作正常,因此不需要任何转换

但是,多边形绘制在PIL图像上,而不是在画布中显示的照片图像上。因此,要查看多边形,需要更新照片图像。在通往d so的路上,使用
ImageTk.PhotoImage
.paste
方法

以下是带有更改的
draw\u poly
函数:

def draw_poly(event):
    numberofPoint = len(list_of_points)
    if numberofPoint>2:
        draw.polygon(list_of_points, outline=(255, 0, 0))  # replaced poly by list_of_points
        canvas.old_coords = None
        canvas.image.paste(img)   # update displayed image
        list_of_points[:]=[]

当我将点列表直接传递给draw.polygon时,它工作正常,因此不需要任何转换

但是,多边形绘制在PIL图像上,而不是在画布中显示的照片图像上。因此,要查看多边形,需要更新照片图像。在通往d so的路上,使用
ImageTk.PhotoImage
.paste
方法

以下是带有更改的
draw\u poly
函数:

def draw_poly(event):
    numberofPoint = len(list_of_points)
    if numberofPoint>2:
        draw.polygon(list_of_points, outline=(255, 0, 0))  # replaced poly by list_of_points
        canvas.old_coords = None
        canvas.image.paste(img)   # update displayed image
        list_of_points[:]=[]

你做了一个
打印(poly)
,我们可以得到这个变量的内容吗?zip(点列表)然后poly中的值是:[(411113),((158169),,,((3443364),)]poly=tuple(点列表)poly中的值是:((43224),(196245),(268379)),没有错误,但我没有画多边形。@john多边形是在PIL图像上画的,不在画布中显示的
PhotoImage
上,这就是为什么我们看不到它的原因。如何在PhotoImage上为我处理canvas和PIL的这个特殊程序获取它?你做了一个
打印(poly)
,我们可以得到这个变量的内容吗?zip(点列表)那么poly中的值是:[(411113),(158169),)((344364),)]poly=元组(点的列表)poly中的值是:((43224),(196245),(268379))没有错误,但我没有绘制多边形。@john多边形是在PIL图像上绘制的,而不是在画布上显示的
PhotoImage
上,这就是为什么我们看不到它的原因。如何在PhotoImage上为我同时处理画布和PIL的这个特殊程序获取它?当我打印\u点列表的内容时,我得到的结果:[266,126],[154238],[350266]],如何将其转换为此结构:[(43224),(196245),(268379)]类似这样的操作将
[(i[0],i[1])用于x中的i]
当我打印_点列表的内容时,我得到了结果:[[266126],[154238],[350266]],如何将其转换为此结构:[(43224),(196245),(268379)]类似这样的操作将对x中的i执行
[(i[0],i[1])