2016-09-14 45 views
0

我看到了多個問題,但無法找到我的問題的答案。基本上我只想在圖像上畫一條線,從python的外部文件中獲取座標。而在這裏不用我的代碼:ImageDraw.draw.line():SystemError:新樣式getargs格式,但參數不是元組

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 

如果我硬編碼的價值觀,我看沒問題。問題只發生在上述情況。任何人都可以指出問題嗎?

+0

你所示的代碼,你看到的錯誤是不一樣的。這條線在哪裏:'draw.line(([x,y],[endx,endy]),fill = 1)'? – karthikr

+0

更新了錯誤。基本上錯誤是在最後一行之前。你可以討論什麼是問題? – user3193684

回答

0

看起來只有幾個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() 
1

此消息通常意味着人們試圖在元組期望傳遞單獨的值。

儘管pillow doc

xy – Sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...].

你應該堅持這個版本:

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