使用Python制作生成二维码

使用Python制作生成二维码

-- coding:utf-8 --

author = “HouZhipeng”
blog = “https://blog.csdn.net/Zhipeng_Hou

import os

import qrcode
from PIL import Image
from pyzbar import pyzbar

def make_qr_code_easy(content, save_path=None):
“”"
Generate QR Code by default
:param content: The content encoded in QR Codeparams
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
“”"
img = qrcode.make(data=content)
if save_path:
img.save(save_path)
else:
img.show()

def make_qr_code(content, save_path=None):
“”"
Generate QR Code by given params
:param content: The content encoded in QR Code
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
“”"
qr_code_maker = qrcode.QRCode(version=2,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=8,
border=1,
)
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()

def make_qr_code_with_icon(content, icon_path, save_path=None):
“”"
Generate QR Code with an icon in the center
:param content: The content encoded in QR Code
:param icon_path: The path of icon image
:param save_path: The path where the generated QR Code image will be saved in.
If the path is not given the image will be opened by default.
:exception FileExistsError: If the given icon_path is not exist.
This error will be raised.
: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=4,
                              error_correction=qrcode.constants.ERROR_CORRECT_H,
                              box_size=8,
                              border=1,
                              )
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)
else:
    qr_code_img.show()

def decode_qr_code(code_img_path):
“”"
Decode the given QR Code image, and return the content
:param code_img_path: The path of QR Code image.
:exception FileExistsError: If the given code_img_path is not exist.
This error will be raised.
:return: The list of decoded objects
“”"
if not os.path.exists(code_img_path):
raise FileExistsError(code_img_path)

# Here, set only recognize QR Code and ignore other type of code
return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE])

if name == “main”:

make_qr_code_easy("make_qr_code_easy", "qrcode.png")
results = decode_qr_code("qrcode.png")
if len(results):
    print(results[0].data.decode("utf-8"))
else:
    print("Can not recognize.")

make_qr_code("make_qr_code", "qrcode.png")
results = decode_qr_code("qrcode.png")
if len(results):
    print(results[0].data.decode("utf-8"))
else:
    print("Can not recognize.")

make_qr_code_with_icon("https://blog.csdn.net/Zhipeng_Hou", "dg4.jpg", "qrcode.png")
results = decode_qr_code("qrcode.png")
if len(results):
    print(results[0].data.decode("utf-8"))
else:
    print("Can not recognize.")

这个是我从别人那边download下来的。生成的是这样的
在这里插入图片描述
这个会比较复杂并且是静态的.

下面这个会比较简单,并且可以做成动态GIF格式的
from MyQR import myqr
import os
words=“https://blog.csdn.net/Bdingse/article/details/89219172” # 给一个你要的地址
version, level, qr_name = myqr.run(
words,
version=1,
level=“H”,
picture=“dg4.jpg”, # 你提供的图片,要确保图片和代码在统一路径下
colorized=True, # true则是彩色的,false的话就是黑白
contrast=1.2, #对比度
brightness=1.2, #亮度
save_name=“p4.gif”, # 生成的二维码,
save_dir=os.getcwd() # 保存
)
结果:

z在这里插入图片描述
参考地址:
第一个:https://blog.csdn.net/Zhipeng_Hou/article/details/83381133
第二个:https://blog.csdn.net/qq_40833182/article/details/81459333

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值