Python Two-dimensional Code generation library QRCode installation and use example _python

Source: Internet
Author: User
Tags response code

Two-dimensional code for short QR code (quick Response Code), scientific name for the fast Response matrix code, is a two-dimensional barcode, by the Japanese Denso Wave Company in 1994 invented. Now with the popularity of smartphones, has been widely used in ordinary life, such as commodity information inquiries, social interaction friends, network address access and so on.

Install Python's two-dimensional code library--QRCode

Since you need to rely on the Python image Library to generate QRCode pictures, you will need to install the Python Image library pil (Python Imaging library), or you may encounter "Importerror:no module named image" Error:

Copy Code code as follows:

sudo easy_install pil

If you install PIL, you receive the following error:

Copy Code code as follows:

_imagingft.c:73:10:fatal error: ' freetype/fterrors.h ' File not found
#include <freetype/fterrors.h>
^
1 Error generated.
Error:setup script exited with Error:command ' CC ' failed with exit status 1

Found on the StackOverflow is the MAC depends on the FreeType link change problem, resolved as follows:

Copy Code code as follows:

Ln-s/usr/local/include/freetype2/usr/local/include/freetype
sudo easy_install-u pil

To install the QRCode library:

Copy Code code as follows:

sudo easy_install qrcode

After a successful installation, the QR command can be used to generate a two-dimensional code in the terminal:

Copy Code code as follows:

QR "Just a Test" > Test.png
QR--help

Sample code

Copy Code code as follows:

Import QRCode


qr = QRCode. QRCode (
version=2,
error_correction=qrcode.constants.error_correct_l,
BOX_SIZE=10,
Border=1
)
Qr.add_data ("http://jb51.net/")
Qr.make (Fit=true)
img = Qr.make_image ()
Img.save ("Dhqme_qrcode.png")

The parameter version represents the size of the generated two-dimensional code, the value range is 1 to 40, the minimum size 1 generates a 21 * 21 two-dimensional code, version each increment of 1, the resulting two-dimensional code will add 4 dimensions, such as version is 2, then generate 25 * 25 two-dimensional code 。

The parameter error_correction specifies the fault-tolerant coefficient of the two-dimensional code, which has the following 4 coefficients:

1.error_correct_l:7% 's codewords can be fault tolerant
2.error_correct_m:15% 's codewords can be fault tolerant
3.error_correct_q:25% 's codewords can be fault tolerant
4.error_correct_h:30% 's codewords can be fault tolerant

The parameter box_size represents the pixel size of each lattice in a two-dimensional code.

The parameter border indicates how much the border's lattice thickness is (by default, 4).

Running the code above will generate a QR code for our station:

Generate a two-dimensional code with an icon

The higher the fault-tolerant coefficient of a two-dimensional code (the error_correction indicated above), the resulting two-dimensional code can allow a greater percentage of the deformity, and two-dimensional code data is mainly stored in the image of the four corner, so in the middle of the two-dimensional code to put a small icon, the two-dimensional code recognition is not affected by how much.

For the size of the icon inserted on the two-dimensional code, this specifies that the size of the limit icon is a maximum of 1/4 of the two-dimensional code length, so that the defect is too large to affect recognition.

Finally, combined with the Python Image Library (PIL) operation, the picture stickers (paste) in the middle of two-dimensional code picture, you can generate a two-dimensional code with an icon, the specific operation code is as follows:

Copy Code code as follows:

Import Image
Import QRCode


qr = QRCode. QRCode (
version=2,
Error_correction=qrcode.constants.error_correct_h,
BOX_SIZE=10,
Border=1
)
Qr.add_data ("http://jb51.net/")
Qr.make (Fit=true)

img = Qr.make_image ()
img = Img.convert ("RGBA")

icon = Image.open ("Favicon.png")

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:
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)
Img.paste (Icon, (W, h), icon)

Img.save ("Dhqme_qrcode.png")

PS: This site also provides a very powerful two-dimensional code generation tool, interested friends can refer to:

Http://tools.jb51.net/transcoding/jb51qrcode

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.