Python:创建二维码

class QRCodeUtil:
    @staticmethod
    def make_qr_code(content, save_path=None):
        """

        :param content: 二维码内容
        :param save_path: 生成的二维码的保存的路径
        :return:
        """
        qr_code_maker = qrcode.QRCode(version=5,
                                      error_correction=qrcode.constants.ERROR_CORRECT_M,
                                      box_size=8,
                                      border=4,
                                      )
        qr_code_maker.add_data(data=content)
        qr_code_maker.make(fit=True)
        img = qr_code_maker.make_image(fill_color="black", back_color="white")
        if save_path:
            img.save(save_path)
        else:
            img.show()  # 中间图不显示

    @staticmethod
    def make_qr_code_with_icon(content, icon_path, save_path=None):
        """

        :param content: 二维码内容
        :param icon_path: 图标的路径
        :param save_path: 生成的二维码的保存路径
        :return:
        """
        if not os.path.exists(icon_path):
            raise FileExistsError(icon_path)

        # First, generate an usual QR Code image
        qr_code_maker = qrcode.QRCode(version=5,
                                      error_correction=qrcode.constants.ERROR_CORRECT_H,
                                      box_size=8,
                                      border=4,
                                      )
        qr_code_maker.add_data(data=content)
        qr_code_maker.make(fit=True)
        qr_code_img = qr_code_maker.make_image(
            fill_color="black", back_color="white").convert('RGBA')

        # Second, load icon image and resize it
        icon_img = Image.open(icon_path)
        code_width, code_height = qr_code_img.size
        icon_img = icon_img.resize(
            (code_width // 4, code_height // 4), Image.ANTIALIAS)

        # Last, add the icon to original QR Code
        qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))

        if save_path:
            qr_code_img.save(save_path)  # 保存二维码图片
            qr_code_img.show()  # 显示二维码图片
        else:
            return qr_code_img

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值