Python QR code generation library qrcode installation and use example

Source: Internet
Author: User
This article describes how to install and use the Python QR code generator Library qrcode. This article describes how to install qrcode, generate a QR code, and generate a QR code with an icon, you can refer to the QR Code (Quick Response Code), which is a two-dimensional Code developed by Japan's Denso Wave company in 1994. Nowadays, with the popularity of smartphones, it has been widely used in daily life, such as commodity information query, social interaction with friends, and network address access.

Install the Python QR code library-qrcode

To generate a qrcode Image, you need to install Python Image Library PIL (Python Imaging Library) first. Otherwise, you will encounter the error "ImportError: No module named Image:

The code is as follows:


Sudo easy_install pil

If the following error occurs during pil installation:

The code is as follows:


_ Imagingft. c: 73: 10: fatal error: 'freetype/fterrors. h' file not found
# Include
^
1 error generated.
Error: Setup script exited with error: command 'CC' failed with exit status 1

On StackOverflow, we found that the FreeType link on Mac is changed. the solution is as follows:

The code is as follows:


Ln-s/usr/local/include/freetype2/usr/local/include/freetype
Sudo easy_install-U pil

Install the qrcode Library:

The code is as follows:


Sudo easy_install qrcode

After the installation is successful, you can use the qr code command in the terminal to generate a qr code:

The code is as follows:


Qr "Just a test"> test.png
Qr -- help

Sample code

The code is 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 indicates the size of the generated QR code. the value range is 1 to 40. the minimum size 1 generates a 21*21 QR code, the generated QR code is added with four dimensions. for example, if the version is 2, a 25x25 QR code is generated.

The error_correction parameter specifies the fault tolerance coefficient of the QR code, which has the following four coefficients:

1. Error tolerance for ERROR_CORRECT_L: 7% characters
2. Error tolerance for ERROR_CORRECT_M: 15% characters
3. Error tolerance of ERROR_CORRECT_Q: 25% characters
4. Error tolerance of ERROR_CORRECT_H: 30% characters

The box_size parameter indicates the pixel size of each grid in the QR code.

The border parameter indicates the grid thickness of the border (default value: 4 ).

Run the above Code to generate a QR Code for the website:

Generate a QR code with an icon

The higher the fault tolerance coefficient of the QR code (error_correction mentioned above), the larger the fault tolerance rate of the generated QR code, and the data of the QR code is mainly stored on the four corners of the image, therefore, placing a small icon in the middle of the QR code does not affect the identification of the QR code.

For the icon size inserted on the QR code, the maximum size of the restricted icon is 1/4 of the length and width of the QR code.

Finally, combined with the Python Image Library (PIL) operation, paste the image paste in the middle of the QR code image to generate a QR code with an icon. the specific operation code is as follows:

The code is 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 ")

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.