我被赋予了创建旗帜图像的任务。到目前为止,我已经完全找到了关于正确着色旗帜的部分,但我无法弄清楚如何处理尺寸参数。
我必须使用单个参数“d”表示标志的尺寸。然后以某种方式引用该参数来创建宽度和高度的变量。我不确定如何处理这一部分。
from PIL import Image, ImageDraw
def tri_vertical( d, c1, c2, c3 ) :
''' Return a new flag of dimension d and equally-spaced colors
c1, c2, and c3'''
# create image of appropriate size
d = (WIDTH, HEIGHT)
BACKGROUND_COLOR = "black"
im = Image.new( 'RGB', (WIDTH, HEIGHT), BACKGROUND_COLOR )
# get a drawable canvas of image im
canvas = ImageDraw.Draw( im )
x = 0
y = 0
wi = (1/3)*WIDTH
h = HEIGHT
xy = [ (x, y), (x + wi, y + h) ]
canvas.rectangle( xy, fill=c1 )
x = (1/3)*WIDTH
y = 0
wi = (1/3)*WIDTH
h = HEIGHT
xy = [ (x, y), (x + wi, y + h) ]
canvas.rectangle( xy, fill=c2 )
x = (2/3)*WIDTH
y = 0
wi = (1/3)*WIDTH
h = h
xy = [ (x, y), (x + wi, y + h) ]
canvas.rectangle( xy, fill=c3)
return im
答案 0 :(得分:1)
您的代码几乎是正确的。只需替换
d = (WIDTH, HEIGHT)
与
WIDTH, HEIGHT = d
你会很高兴。