使用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下来的。生成的是这样的
使用Python制作生成二维码_第1张图片
这个会比较复杂并且是静态的.

下面这个会比较简单,并且可以做成动态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使用Python制作生成二维码_第2张图片
参考地址:
第一个:https://blog.csdn.net/Zhipeng_Hou/article/details/83381133
第二个:https://blog.csdn.net/qq_40833182/article/details/81459333

你可能感兴趣的:(使用Python制作生成二维码)