PYTHON生成图片实现过程
实现过程 ,首先需要2张图片。头部、 水印二张图片。
#先生成一张空白图片。并在上面划线,以及标题的灰色背景
im = Image.new('RGB', (460, 510), 0xffffff)
draw = ImageDraw.Draw(im)
width,height = im.size
#画行横线,每行30像素,第一行多10像素。
for i in range(1,17):
y=i*30+10;
draw.line(((10, y),(width-10,y)) , fill=(225,225,225))
#边线
draw.line(((10, 1),(10,height-20)) , fill=(225,225,225))
draw.line(((width-10, 1),(width-10,height-20)) , fill=(225,225,225))
#标题
#标题背景
draw.rectangle(((10,1),(width-10,40)),fill=(223,223,223));
#标题文字,使用雅黑粗
font = ImageFont.truetype(self.root+/inc/font/msyhbd.ttc,14)
fontcolor = (14,77,157)
#draw = ImageDraw.Draw(im)
#draw.text((20,10), No., fill=fontcolor,font=font)
draw.text((20,10), manufacturer, fill=fontcolor,font=font)
draw.text((140,10), model, fill=fontcolor,font=font)
draw.text((280,10), count, fill=fontcolor,font=font)
#new_img = im.resize((460,700),Image.BILINEAR)
#查数据库
sql=............................缩。。。“
result=self.srsir.getAll(sql);
i=1;
font = ImageFont.truetype(self.root+/inc/font/msyh.ttc,12)
fontcolor= (50,100,100);
for row in result:
manufacturer,model,score=row;
manufacturer=self.rep(manufacturer);
model=self.rep(model)
if model.find(manufacturer)!=-1:
model=model.replace(manufacturer, '').lstrip();
if len(model)>40:
model=model[:40].strip()+'..'
model=model.replace(' ',' ')
#写入行数据。
y=i*30+15;
draw.text((20,y), manufacturer, fill=fontcolor,font=font)
draw.text((140,y), model, fill=fontcolor,font=font)
draw.text((280,y), str(num1), fill=fontcolor,font=font)
if rank!=0:
s=↑ if rank>0 else ↓
draw.text((350,y),unicode(s,'utf-8') , fill=(50,150,0) if rank<0 else="" (150,10,10),font=""></0>
draw.text((360,y), str(rank), fill=(50,150,0) if rank<0 else="" (150,10,10),font=""></0>
im.save(self.root+/%s%outfile)
#合并头部图片,并加水印,略。。。。。
self.mergeImg([%s_header.png%outType,outfile],outfile,title)
if self.debug:
Image.open(outfile).show();
更多生成图片: