Import readlineimport qrcodefrom pil import imageimport osdef create_ QRCode (strings,path,logo= ""): qr = qrcode. QRCode ( version=2, error_correction=qrcode.constants.error_correct_l, box_ size=6, border=1, ) with open (' Test.txt ', ' R ') as fr: for Lines in fr.readlines (): Lines = lines.replace ("\ n", ""). Strip () id = lines lines = strings + lines &nbsP; if len (lines) > 0: qr.add_data (lines) qr.make (fit=True) img = qr.make_ Image () img = img.convert ("RGBA") if os.path.exists (PATH) and os.path.isfile (logo): icon = image.open (Logo) img_w,img_h = img.size factor = 5 size_w = int (img_w / factor) size_h = int (Img_h / factor) 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) 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 ( ID + '. jpg ') if __name__ == "__main__": create_qrcode (' Http://hepaidai.com/?channel_ Code=hpd&sub_id= ', ' E:\PythonProject\\test ', ' E:\PythonProject\\test\hpd.jpg ')
Description
Third-party libraries need to be installed: QRCode, PIL, Image (PIP installation recommended) strings: QR Code string path: generated QR code save path Logo: Logo file to add
Generated two-bit code:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/71/59/wKioL1XMRTuj1WGlAABp7Oft7GI482.jpg "title=" Two-dimensional code with logo "alt=" wkiol1xmrtuj1wglaabp7oft7gi482.jpg "/>
Note: It is recommended to set the logo a little bit otherwise the two-dimensional code can not be recognized
This article is from the Linux tours blog, so be sure to keep this source http://openlinuxfly.blog.51cto.com/7120723/1684354
Python makes two-dimensional code and can add logos