Tao's Python script toolbox generates two-dimensional code with logo

Source: Internet
Author: User
Tags response code

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 for short (quick Response code), the scientific name for the fast Response matrix code, is a two-dimensional barcode, by the Japanese Denso Wave company invented in 1994. Now with the popularization of smart phones, has been widely used in ordinary life, such as commodity information query, social Friends interaction, network address access and so on.

Install Python's QR code library--QRCode

Pip Install QRCode

Because the qrcode images need to depend on the Python image Library, you will need to install the Python Image Library PIL (Python Imaging Library) before you 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 needs to be installed 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

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.