栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

二维码生成

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

二维码生成

QR Code

Quick Response Code

用某种特定的几何图形按一定规律在平面(二维方向上)分布的、黑白相间的、记录数据符号信息的图形。

功能
  • 信息获取
  • 网站跳转
  • 广告推送
  • 防伪溯源
  • 账号登录
  • 手机支付
矩阵式

位置探测图形 协助扫描软件定位 QR 码并转换坐标系。

校正图形 用于进一步校正坐标系。

定位图形 用于指示标识密度和确定坐标系。

格式信息 存放一些格式化数据的信息。

版本信息 规定二维码的规格。

数据和纠错码 存储实际数据以及用于纠错码字。

位置探测图形分隔符 区分功能图形和编码区域。

蒙版图案 解决大面积的空白或黑块的情况。

Python 生成 QR Code qrcode

pip install qrcode, Image

import qrcode

# 创建QRCode对象
qr = qrcode.QRCode(
	version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_H,
    box_size=40,
    border=4
)
# 添加数据
qr.add_data('https://www.baidu.com')
# 填充数据
qr.make(fit=True)
# 生成图片
img = qr.make_image(fill_color="black", back_color="white")
添加LOGO图片
from PIL import Image

# 转换成RGBA格式
img = img.convert('RGBA')
ico = Image.open('favicon.png')	# 你本地的图片
# 修改图片大小
ico = ico.resize((int(img.size[0]/3),int(img.size[1]/3)), Image.ANTIALIAS)
ico = ico.convert('RGBA')
img_center_axis = (int((img.size[0]-ico.size[0])/2),int((img.size[1]-ico.size[1])/2))
img.paste(ico, img_center_axis, ico)
预览图片
from matplotlib import pyplot as plt

# 预览图片
plt.imshow(img)
plt.show() 
# 保存图片
# img.save('baidu.png')
带个性化背景图
import qrcode
from PIL import Image
from matplotlib import pyplot as plt


whiteRGBA = (255,255,255,255)  # 白色RGB值
blackRGBA = (0,0,0,255)        # 黑色RGB值

class GenerateQRCode(object):
    def __init__(self, background_map, version=5, error_correction=qrcode.ERROR_CORRECT_H, box_size=8, border=2):
        self.version = version                        # 版本信息
        self.error_correction = error_correction      # 容错率
        self.box_size = box_size                      # 二维码位置确定的点数
        self.border = border                          # 边框
        self.background_map = background_map          # 背景图
        
    def __transparent_back(self,image):
        """白色背景透明设置"""
        img = image.convert('RGBA')
        W, H = img.size
        color_0 = whiteRGBA   # 要替换的颜色
        for h in range(H):
            for w in range(W):
                dot = (w,h)
                color_1 = img.getpixel(dot)
                if color_1 == color_0:
                    color_1 = color_1[:-1] + (0,)
                    img.putpixel(dot,color_1)
        return img
    
    def __position(self,coordinate):
        """计算二维码黑点白点位置"""
        Tlist = list()
        Flist = list()
        rnum = int(self.box_size/(self.box_size/2))
        clen = len(coordinate)
        numy = 0
        for y in coordinate:
            if self.border - 1 < numy < self.box_size + self.border:
                numx = 0
                for x in y:
                    if  self.box_size + self.border <= numx < clen - (self.box_size + self.border): 
                        if x == True: 
                            # 计算出坐标位置
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Tlist.append((wx,hy))
                        elif x == False:
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Flist.append((wx,hy))
                    numx += 1
            elif self.box_size + self.border <= numy < clen - (self.box_size + self.border):
                numx = 0
                for x in y:
                    if self.border - 1 < numx < clen - self.border:
                        if x == True:
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Tlist.append((wx,hy))
                        else:
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Flist.append((wx,hy))
                    numx += 1
            elif clen - (self.box_size + self.border) <= numy < clen - self.border:
                numx = 0
                for x in y:
                    if  self.box_size + self.border <= numx < clen - self.border : 
                        if x == True:
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Tlist.append((wx,hy))
                        elif x == False:
                            wx = int(numx*self.box_size) + rnum
                            hy = int(numy*self.box_size) + rnum
                            Flist.append((wx,hy))
                    numx += 1
            numy += 1
        return Tlist,Flist
         
    def Qrcode(self, txt):
        """生成二维码"""
        qr = qrcode.QRCode(
            version = self.version,
            error_correction = self.error_correction,
            box_size = self.box_size,
            border = self.border,
        )
        qr.add_data(txt)
        qr.make(fit=True)
        # 获取二维码矩阵
        coordinate = qr.get_matrix()
        # 获取生成的二维码大小
        qrimg = qr.make_image()
        qrimg = self.__transparent_back(qrimg)
        w, h = qrimg.size
        # 打开背景图
        b_map = Image.open(self.background_map)
        # 设置背景图大小
        b_map_image = b_map.resize((w, h), Image.ANTIALIAS)
        # 获取黑点和白点的列表
        (Tlist, Flist) = self.__position(coordinate)
        '''
        1、把需要缩小的黑色块变成白色
        2、画出黑色的小块
        3、把图片白色透明
        4、二维码图和背景图合并
        5、画出白色的小方块
        '''

        rnum = int(self.box_size/(self.box_size/2))
        for n in Tlist:
            for m in range(self.box_size + 1):
                for m1 in range(self.box_size + 1):
                    qrimg.putpixel((n[0]-rnum + m1, n[1]-rnum + m),(255,255,255,255))
            for p in range(rnum + 2):
                for p1 in range(rnum + 2):
                    qrimg.putpixel((n[0] + p1, n[1]+p),(0,0,0,255))
        
        image = self.__transparent_back(qrimg)
        for n1 in Flist:
            for p in range(rnum + 2):
                for p1 in range(rnum + 2):
                    image.putpixel((n1[0] + p1, n1[1]+p),(255,255,255,255))
        # 保存图片
        b_map_image.paste(image, (0, 0), image)
        plt.imshow(b_map_image)
        plt.show()
        # b_map_image.save(save_file, quality=100)

if __name__ == "__main__":
    gqrc = GenerateQRCode(background_map='background.jpg')
    gqrc.Qrcode("http://wwww.baidu.com")

qrcode · PyPI

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号