建议在Python2.x版本食用
webapp.py
#!/usr/bin/env python# -*- coding:UTF-8 -*-import webimport qrcodefrom PIL import Imageimport datetimeurls = ( '/','Index' # '/images/logo.png','Logo' #可放于静态文件夹static中)render = web.template.render('templates')#模板引擎class Index: def GET(self):#函数名是当前请求方式 return render.index() #return open('templates/index.html','rb').read() def POST(self): #获取前台请求的数据参数 i = web.input() #获取请求的数据 return getcode(i)# class Logo: # def GET(self): # return open('images/logo.png','rb').read()def getcode(info): qr = qrcode.QRCode( version = 1, error_correction = qrcode.constants.ERROR_CORRECT_Q, #容错比,比例越大可被遮挡部分越多 box_size = 10, border = 4, )#实例一个qrcode对象 qr.add_data( ''' BEGIN:VCARD\n VERSION:3.0\n URL:%s\n END:VCARD ''' # FN:%s\n # ORG:%s\n # TITLE:%s\n # ADR;WORK:%s\n # TEL;WORK:%s\n # EMAIL;WORK:%s\n # URL:%s\n # NOTE:%s\n %(info['url']) # %(info['name'],info['company'],info['title'],info['address'],info['mobile'],info['email'],info['url'],info['desc']) ) img = qr.make_image()#创建二维码图片 类型:image img.convert("RGBA") img_w,img_h = img.size icon = Image.open('static/images/logo1.png') icon_w = img_w/4 icon_h = img_h/4 logo = icon.resize((icon_w,icon_h),Image.ANTIALIAS) w = (img_w-icon_w)/2 h = (img_h-icon_h)/2 img.paste(logo,(w,h)) date = datetime.datetime.now().strftime('%Y-%m-%d %H`%M`%S') # #time.strf('%Y-%m-%d %H`%M`%S') path = 'static/imgcard/%s.jpg'%date img.save(path) return pathif __name__ == '__main__': app = web.application(urls,globals()) app.run()
index.html
微信网址二维码--李狗嗨 网址生成二维码