This article mainly introduces the Python implementation of the text generation of two-dimensional code method, combined with a complete example of Python generated two-dimensional code operation of the specific steps and related implementation skills, the need for friends can refer to the next
This article describes the Python implementation of the text generation of two-dimensional code method. Share to everyone for your reference, as follows:
#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# generate two-dimensional code picture 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:1 5% q:25% h:30% box_size=10, #每个格子的像素大小 border=2, #边框的格子宽度大小) qr.add_data (str) qr.make (fit=true) Img=qr.make_ima GE () img.save (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.ex ists (Logo): Icon=image.open (logo) #获取二维码图片的大小 img_w,img_h=img.size factor=4 size_w=int (img_w/factor) siz E_h=int (Img_h/factor) #logo图片的大小不能超过二维码图片的1/4 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) #保存处理后图片 I Mg.save (Save) if __name__== ' __main__ ': save_path= ' theqrcode.png ' #生成后的保存文件 logo= ' logo.jpg ' #logo图片 str=raw_input (' Please enter the text content to generate the QR code: ') #make_qr (str) MAKE_LOGO_QR (Str,logo,save_path)