Python generates two-dimensional code

Source: Internet
Author: User

Python is powerful because he has a strong library of classes, whether it's for data analysis or scientific computing, for web development, and so on.

There is also a concise language, (I have always felt that if the simple popular language can understand a complex truth, that you have thoroughly understood this matter).

In this language, Python is able to use concise code to achieve complex functions, and it is easy to understand and grasp, but no matter what language programming, the end is to play

The idea of ....
Today, let's talk about Python generation of the most popular QR code now.

1, first you need to install two class library

Pip install QRCode #是个用来生成二维码图片的第三方模块, dependent on PIL module

Pip Install Pillow

2, the following is a simple code, if the content is not more than 2331 (I own the maximum length of the test, do not know why the Internet is said to be 1108).

After generating the QR code image, we can read the content directly using the mobile phone scan code

#!/usr/bin/env python#-*-coding:utf-8-*-import qrcodestr_data = "' Here is the QR Code content test, the maximum length of the content 2331 ' Print len (str_data) img = Qrcode.make (Str_data) img.save ('/users/gide/downloads/erweima.jpg ')

3, the following is more complete with the logo of the two-dimensional code package code. We generate a QR code that searches for a book directly

Logo image

  

#!/usr/bin/env python#-*-coding:utf-8-*-import sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') "" "Create a QR code with logo" " Import qrcodefrom PIL import imageimport osdef gen_qrcode (String, Path, logo= ""): "" "Generate two-dimensional code with logo in the middle need to install QRCode, P Il library:p Aram string: Two-dimensional code string:p Aram path: generated QR code save path:p Aram Logo:logo file path: return: "" "QR = QRCode.    QRCode (version=6, 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.a Ntialias) 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__": Gen_qrcode (' http://www.kindlepush.com/search/?sKey=%E6%B0%91%E5%9B%BD%E5%BA% 95%e8%89%b2 ', "/users/gide/downloads/erweima.png", "logo.jpg")

Parameter description:

Parameter Version Description:
Indicates the size of the generated QR code, the value range is 1 to 40, the minimum size 1 will generate 21 * 21 of the QR code,
With each increment of version 1, the resulting QR code will add 4 dimensions,
For example, if version is 2, a QR code of 25 * 25 is generated.
The parameter error_correction specifies the fault tolerance coefficients for the two-dimensional code, respectively, with the following 4 coefficients:
1.error_correct_l:7% Loadline can be fault-tolerant
2.error_correct_m:15% Loadline can be fault-tolerant
3.error_correct_q:25% Loadline can be fault-tolerant
4.error_correct_h:30% Loadline can be fault-tolerant
The parameter box_size represents the pixel size of each lattice in the QR code.
The parameter border indicates how much the border's lattice thickness is (by default, 4).
After running the code, a erweima.png QR code is generated in the current directory of the code, which can be directly redirected to the search page by scanning the phone.

So now the two-dimensional code on the shared bike is actually a request address, you sweep the code will send parameters to request their server, after a series of validation on the service side, it will

Start the unlocking function. Of course Ofo this kind of bike is probably just the beginning of a direct implementation of the generated good random number, returned to you, and then you can use. Of course really actual

The application is more complex.

over!

Python generates two-dimensional code

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.