QR Code is also known as two-dimensional Bar Code, the common TWO-DIMENSIONAL Code is QR Code, QR full name Quick Response, is a super popular coding method on mobile devices in recent years, it can store more information than the traditional Bar Code, but also can represent more data types.

If we share content, directly send each other a TWO-DIMENSIONAL code or display two-dimensional code on their own website, is not more forced. Here we show you how to use Python to generate a QR code and display it on our website.

PIP install qrcode PIP install myqr

Qrcode library use

def gen_qrcode(text): Qrcode.qrcode (version=2, error_correction=qrcode.constants.ERROR_CORRECT_Q, box_size=10, border=2, Img = qr.make_image() # convert the image to RGBA format img = img.convert('RGBA') # return the size of the image of the qr code img_w, Img_h = img.size # Open logo = image.open (os.path.join(BASE_DIR, Logo_w = img_w // 4 loGO_h = img_w // 4 # Change the size of the logo image logo = logo.resize((logo_w, logo_h), W = (img_w-logo_W) // 2 h = (img_h-loGO_h) // 2 img.paste(logo, (w, h)) qr_path = 'static/imgcard/%s.png' % time.time() img.save(os.path.join(BASE_DIR, qr_path)) return qr_pathCopy the code

Parameter meanings are as follows: version: the value is an integer ranging from 1 to 40, which controls the size of the TWO-DIMENSIONAL code (the minimum value is 1, which is a 12 x 12 matrix) error_correction: controls the error correction function of the two-dimensional code. The following four constants can be taken. ERRORCORRECTL: Approximately 7% or less of errors can be corrected. ERRORCORRECTM (default) : Approximately 15% or less of errors can be corrected. RORCORRECTH: Approximately 30% or less of errors can be corrected. Box_size: Controls the number of pixels contained in each cell of the TWO-DIMENSIONAL code. Border: Controls the number of squares in the border (the distance between the QR code and the image boundary) (default is 4, which is the minimum value specified by relevant standards)

Myqr libraries use

From MyQR import MyQR myqr.run(words='http://www.baidu.com', # words to fill in the link or text content version=1, # control the size of the TWO-DIMENSIONAL code 1~40, it is recommended not to exceed 5, Picture ='background.gif', # picture='background.gif', # background picture, JPG,.png,.bmp,.gif colorized=True, # contrast=1.0, # contrast=1.0, A smaller value means lower contrast, higher contrast and vice versa. The default is 1.0. Brightness =1.0, # to change the brightness of pictures)Copy the code

Use it on the Django web site

For details, please follow wechat public account: Python crawler Field road, send: qrcode, can obtain GitHub address

If you think my article is ok, you can also scan the following QR code to add my wechat account