python生成二维码并存储数据库

import pymysql
from PIL import Image
import qrcode
#生成二维码工具类型
#根据数据库数据
class kqrcode(object):
    """docstring for ClassName"""
    def __init__(self, arg):
        super(kqrcode, self).__init__()
        #根据数据库数据
        self.arg = arg
    def get_data():
        conn = pymysql.connect(user='root', passwd='admin',host='localhost', db='mytest',charset='utf8')
        cur = conn.cursor()
        #cur.execute("SELECT * FROM jobinfo")
        cur.execute('call travel_gridpager(%s,%s)',(1,4))
        for r in cur:
            config={
     }
            config['id']=str(r[0])
            config['title']=str(r[1])
            print("编号:"+str(r[0])+"  标题:"+str(r[1]))
            url="http://192.168.5.121:8000/feelings/articles/44"+str(r[0])
            #get_erweima(url,str(r[0]));
            kcount=kqrcode.create_qrcode(url,str(r[0]))
            kqrcode.update_data(str(r[0]),"images/"+str(r[0])+".png")
        cur.close()
        conn.close()
    #更新数据库二维码字段
    def update_data(kid,erwei):
        conn = pymysql.connect(user='root', passwd='admin',host='localhost', db='mytest',charset='utf8')
        cur = conn.cursor()
        cur.execute('call travel_erweibyid(%s,%s)',(kid,erwei))
        cur.close()
        conn.close()

    #简单二维码
    def get_erweima(ipa,kid):
        qr=qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_L,box_size=8,border=8,)
        qr.add_data(ipa)
        qr.make(fit=True)
        img=qr.make_image()
        img = img.convert("RGBA")
        img.save("images/"+kid+".png")
    #带logo二维码
    def create_qrcode(url, filename):
        qr = qrcode.QRCode(
            version=1,
            #设置容错率为最高
            error_correction=qrcode.ERROR_CORRECT_H,
            box_size=10,
            border=4,
        )
        qr.add_data(url)
        qr.make(fit=True)
        img = qr.make_image()
        #设置二维码为彩色
        img = img.convert("RGBA")
        icon = Image.open('images/xc.jpg')
        w, h = img.size
        factor = 4
        size_w = int(w / factor)
        size_h = int(h / factor)
        icon_w, icon_h = icon.size
        if icon_w > size_w:
            icon_w = size_w
        if icon_h > size_h:
            icon_h = size_h
        icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
        w = int((w - icon_w) / 2)
        h = int((h - icon_h) / 2)
        icon = icon.convert("RGBA")
        newimg = Image.new("RGBA", (icon_w + 8, icon_h + 8), (249, 205, 173))
        img.paste(newimg, (w-4, h-4), newimg)
        img.paste(icon, (w, h), icon)
        img.save('images/' + filename + '.png', quality=100)
        return 1

你可能感兴趣的:(python)