1. Graphics Library codes captcha
# Introducing graphics library
from captcha.image import ImageCaptcha
# Obtain an image object
image = ImageCaptcha(width=120, height=40, font_sizes=(30, 32, 36))
chars = "1234"
# Create a two-dimensional code objects
im = image.generate(chars)
# (1) direct values
img_val = im.getvalue()
# (2) Save the file
file_name = "1.png"
im.save(file_name)
2. The two-dimensional code library qrcode
import qrcode
import os
# Create a two-dimensional code image err_level 0-3 error in descending size of each pixel cell size
def make_qrcode(url="test", file_name="", size=8, err_level=0):
if err_level == 3:
error_correction = qrcode.constants.ERROR_CORRECT_L
elif err_level == 2:
error_correction = qrcode.constants.ERROR_CORRECT_M
elif err_level == 1:
error_correction = qrcode.constants.ERROR_CORRECT_Q
else:
error_correction = qrcode.constants.ERROR_CORRECT_H
# Initialization code is a two-dimensional objects
qr = qrcode.QRCode(
version=1,
error_correction=error_correction,
box_size=size,
border=4,
)
# Add To generate two-dimensional code connected
qr.add_data(url)
qr.make(fit=True)
file_content = file_name
# Set the fill color and background color
img = qr.make_image(fill_color="black", back_color="white")
# Do you want to save the file
if file_name and isinstance(file_name, str):
# save document
img.save(file_name)
else:
# Direct output
b = io.BytesIO()
img.save(b, "PNG")
file_content = b.getvalue()
return file_content
# Direct call to