【问题标题】:ImageDraw.draw.line() : SystemError: new style getargs format but argument is not a tupleImageDraw.draw.line() : SystemError: new style getargs format but argument is not a tuple
【发布时间】:2016-09-14 18:38:37
【问题描述】:

我看到了多个有关此问题的问题,但无法找到我的问题的答案。基本上我只想在图像上画一条线,从 python 中的外部文件获取坐标。这是我的代码:

import Image, ImageDraw
import sys
import csv
im = Image.open("screen.png")
draw = ImageDraw.Draw(im)
with open("outputfile.txt") as file: 
   reader = csv.reader(file, delimiter=' ')
   for row in reader:
      if row[0] == 'H':
        print "Horizontal line" 
        endx = row[2]
        endy = int(row[3])+int(row[1])
      elif row[0] == 'V':
        print "Vertical line"
        endx = row[2]+row[1]
        endy = row[3]
      x = row[2]
      y = row[3]
      draw.line((x,y, endx,endy), fill = 1)
   im.show()

一切正常,除了这条线:

draw.line((x,y, endx,endy), fill = 1)

我在哪里看到以下错误:

File "dummy_test.py", line 21, in <module>
draw.line((x,y, endx,endy), fill = 1)
File "/Library/Python/2.7/site-packages/PIL-1.1.7-py2.7-macosx-10.10-       intel.egg/ImageDraw.py", line 200, in line
self.draw.draw_lines(xy, ink, width)
SystemError: new style getargs format but argument is not a tuple

如果我对这些值进行硬编码,我认为没有问题。问题仅在上述情况下发生。谁能指出问题所在?

【问题讨论】:

  • 你显示的代码,和你看到的错误不一样。这条线在哪里:draw.line(([x,y], [endx,endy]), fill = 1)
  • 更新了错误。基本上错误在最后一行。请问有什么问题吗?

标签: python


【解决方案1】:

此消息通常意味着人们试图在需要元组的地方传递单独的值。

尽管pillow doc:

xy – 2 元组的序列,如 [(x, y), (x, y), ...] 或数字 [x, y, x, y, ...] 之类的值。

你应该坚持这个版本:

draw.line([(x, y), (endx, endy)], fill = 1)

【讨论】:

    【解决方案2】:

    看起来只有几个 int(...) 丢失了?

    --- a.py.ORIG   2016-09-14 20:54:47.442291244 +0200
    +++ a.py    2016-09-14 20:53:34.990627259 +0200
    @@ -1,4 +1,4 @@
    -import Image, ImageDraw
    +from PIL import Image, ImageDraw
     import sys
     import csv
     im = Image.open("screen.png")
    @@ -8,13 +8,13 @@
        for row in reader:
           if row[0] == 'H':
             print "Horizontal line" 
    -        endx = row[2]
    +        endx = int(row[2])
             endy = int(row[3])+int(row[1])
           elif row[0] == 'V':
             print "Vertical line"
    -        endx = row[2]+row[1]
    -        endy = row[3]
    -      x = row[2]
    -      y = row[3]
    +        endx = int(row[2])+int(row[1])
    +        endy = int(row[3])
    +      x = int(row[2])
    +      y = int(row[3])
           draw.line((x,y, endx,endy), fill = 1)
        im.show()
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多