我在这方面看到了多个问题,但未能找到问题的答案。基本上我只想在图像上绘制一条线,从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
如果我对值进行硬编码,我认为没有问题。问题仅发生在上述情况。有谁可以指出这个问题?
答案 0 :(得分:1)
此消息通常意味着尝试传递预期元组的单独值。
尽管pillow doc:
xy - 像[(x,y),(x,y),...]或数字这样的2元组的序列 值如[x,y,x,y,...]。
你应该坚持这个版本:
draw.line([(x, y), (endx, endy)], fill = 1)
答案 1 :(得分: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()