標籤: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