Python makes two-dimensional code and can add logos

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.