This article has shared the Python text to generate two-dimensional code of the detailed code for your reference, the specific content as follows
Test One: generate text on white bottom black word two-dimensional code picture
Test Two: create a QR code image with a logo
#coding: Utf-8 "python generates two-dimensional code v1.0 main text generation two-dimensional code picture test one: Text generation White bottom black word two-dimensional code picture test two: The text generation with logo of the QR Code image" ' __author__ = ' Xue ' Import Qrcodefrom PIL import imageimport os #生成二维码图片def make_qr (str,save): Qr=qrcode. QRCode (version=4, #生成二维码尺寸的大小 1-40 1:21*21 (21+ (n-1)) error_correction=qrcode.constants.error_correct_m, #L: 7% M:15 % q:25% h:30% box_size=10, #每个格子的像素大小 border=2, #边框的格子宽度大小) qr.add_data (str) qr.make (fit=true) img=qr.make_image () img . Save #生成带logo的二维码图片def MAKE_LOGO_QR (str,logo,save): #参数配置 qr=qrcode. QRCode (version=4, Error_correction=qrcode.constants.error_correct_q, Box_size=8, border=2) #添加转换内容 qr.add_data (str) # Qr.make (fit=true) #生成二维码 img=qr.make_image () # Img=img.convert ("RGBA") #添加logo if logo and os.path.exists (logo): icon =image.open (logo) #获取二维码图片的大小 img_w,img_h=img.size factor=4 size_w=int (img_w/factor) size_h=int (img_h/factor) #log o the size of the picture can not exceed two-dimensional code picture of the Icon_w,icon_h=icon.size if Icon_w>size_w:icon_w=size_w if Icon_h>size_h:icon_h=size_h IcoN=icon.resize ((Icon_w,icon_h), Image.antialias) #计算logo在二维码图中的位置 W=int ((img_w-icon_w)/2) H=int ((Img_h-icon_h)/2) Icon=icon.convert ("RGBA") Img.paste (icon, (w,h), icon) #保存处理后图片 Img.save (Save) if __name__== ' __main__ ': save_path= ' Theqrcode.png ' #生成后的保存文件 logo= ' logo.jpg ' #logo图片 str=raw_input (' Please enter text content to generate QR code: ') #make_qr (str) MAKE_LOGO_QR (str, Logo,save_path)
How does python use the QRCode module to generate a QR code?
1. Introduction
Python-qrcode is a third-party module used to generate two-dimensional code images, depending on the PIL module and the QRCode library.
2. Simple usage
Import qrcode img = qrcode.make (' Hello, QRCode ') img.save (' Test.png ')
3. Advanced usage
Import qrcode QR = QRCode. QRCode ( version=1, error_correction=qrcode.constants.error_correct_l, box_size=10, border=4,) Qr.add_data (' Hello, QRCode ') qr.make (fit=true) img = qr.make_image () img.save (' 123.png ')
4. Parameter meaning:
Version: An integer value of 1~40 that controls the size of the QR code (the minimum value is 1, which is a 12x12 matrix). If you want the program to determine automatically, set the value to None and use the Fit parameter.
Error_correction: Control the error correction function of QR code. The following 4 constants are desirable values.
error_correct_l: About 7% or fewer errors can be corrected.
Error_correct_m (default): Errors of approximately 15% or less can be corrected.
Ror_correct_h: About 30% or fewer errors can be corrected.
Box_size: Controls the number of pixels that each small lattice in the QR code contains.
Border: Control border (the distance between the QR code and the picture boundary) contains the number of squares (the default is 4, which is the minimum value specified by the relevant standard)
I hope this article will help you learn Python programming.