Recently need to add logo on two-dimensional code, online did not find good, so I wrote a python.
Need to install Qrcode,pil library
QR Code (Quick Response code), the scientific name is a high-speed response matrix code, is a two-dimensional barcode. Invented by Denso Wave of Japan in 1994.
Now with the popularity of smartphones. has been widely used in ordinary life, such as product information query, social Friends interaction, network address interview and so on.
Install Python's QR code library--QRCode
Pip Install QRCode
Because generating qrcode images relies on Python's image library, you need to install the Python Image Library PIL (Python Imaging library), or you will encounter the Importerror:no module named image Error.
The code is as follows:
#!/usr/bin/python#-*-encoding:utf-8-*-# Test created on 14-10-28 pm 12:48# Copyright [email protected] "" "Generate with L Ogo qr Code "" "__author__ = ['" "Xitao":<[email protected]> ']import qrcodefrom PIL import imageimport osdef gen_ QRCode (String, Path, logo= ""): "" "generates a two-dimensional code with logo in the middle requires installation of QRCode, PIL library:p Aram string: QR code string:p Aram path: generated QR code Save path:p Aram Logo:logo file path: return: "" "QR = QRCode. QRCode (version=2, Error_correction=qrcode.constants.error_correct_h, Box_size=8, border=1 ) Qr.add_data (String) qr.make (fit=true) img = qr.make_image () img = Img.convert ("RGBA") if logo and Os.path . Exists (logo): Icon = image.open (logo) img_w, img_h = img.size factor = 4 Size_w = Int (img_w/ Factor) Size_h = Int (img_h/factor) icon_w, Icon_h = icon.size if Icon_w > Size_w:ic On_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 (path) if __name__ = = "__main__": <span St Yle= "Font-family:arial, Helvetica, Sans-serif; font-size:12px; " > Gen_qrcode ("http://offbye.com", "Qr.png", "logo.png") </span>
Tao's Python script toolbox generates two-dimensional code with logo