#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: text generation with logo of the two-dimensional code image" " __author__='Xue' ImportQRCode fromPILImportImageImportOS#generate two-dimensional code imagesdefMAKE_QR (str,save): QR=QRCode. QRCode (Version=4,#generate the size of the two-dimensional code 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,#the pixel size of each latticeborder=2,#the size of the lattice width of the border) qr.add_data (str) qr.make (Fit=True) img=qr.make_image () img.save (save)#Create a two-dimensional code image with logodefMAKE_LOGO_QR (str,logo,save):#parameter ConfigurationQr=QRCode. QRCode (Version=4, Error_correction=Qrcode.constants.ERROR_CORRECT_Q, Box_size=8, Border=2 ) #Add Transformation Contentqr.add_data (str)#Qr.make (fit=True)#generate two-dimensional codeimg=qr.make_image ()#Img=img.convert ("RGBA") #Add logo ifLogo andos.path.exists (Logo): Icon=image.open (Logo)#get the size of a two-dimensional code pictureImg_w,img_h=img.size Factor=4Size_w=int (img_w/factor) Size_h=int (img_h/factor)#logo image size can not exceed two-dimensional code image of theIcon_w,icon_h=icon.sizeifIcon_w>Size_w:icon_w=Size_wifIcon_h>Size_h:icon_h=size_h icon=icon.resize ((icon_w,icon_h), Image.antialias)#See: Http://pillow.readthedocs.org/handbook/tutorial.html #calculate the position of the logo in the QR code mapW=int ((img_w-icon_w)/2) H=int ((Img_h-icon_h)/2) icon=icon.convert ("RGBA") Img.paste (icon, (w,h), icon)#See: Http://pillow.readthedocs.org/reference/Image.html#PIL.Image.Image.paste #Save post-processing picturesImg.save (Save)if __name__=='__main__': Save_path='Theqrcode.png' #saved files after the buildlogo='logo.jpg' #logo ImageStr=raw_input ('Please enter the text content to generate the QR code:') #MAKE_QR (str)MAKE_LOGO_QR (Str,logo,save_path)
Python generates two-dimensional code for text