Python製作二維碼,並且可以添加logo

來源:互聯網
上載者:User

標籤:logo   二維碼製作   pyton   

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            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‘)


說明:

需要安裝第三方庫:qrcode ,PIL , Image (推薦使用pip安裝)strings: 二維碼字串path: 產生的二維碼儲存路徑logo: 要添加的logo檔案


產生的二位碼:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/71/59/wKioL1XMRTuj1WGlAABp7Oft7GI482.jpg" title="帶logo的二維碼" alt="wKioL1XMRTuj1WGlAABp7Oft7GI482.jpg" />

注意:建議把logo設定的小一點否則製作的二維碼不能夠被識別

本文出自 “Linux之旅” 部落格,請務必保留此出處http://openlinuxfly.blog.51cto.com/7120723/1684354

Python製作二維碼,並且可以添加logo

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.