Share Python text to generate a QR code instance

Source: Internet
Author: User
This article mainly introduces the Python text generation QR code instance, which has some reference value. Interested friends can refer to the example in this article and share the Python text generation QR code details, for your reference, the specific content is as follows:

Test 1:Generate a QR code image with white background and black characters
Test 2:Generate a QR code image with a logo

# Coding: UTF-8 ''' Python generates a QR code v1.0. Generate a text QR code. Image Test 1: generate a text with a white background and a black text QR code. Image Test 2: generate a QR code image ''' _ author _ = 'xue 'import qrcodefrom PIL import Imageimport OS # generate a QR code image def make_qr (str, save ): qr = qrcode. QRCode (version = 4, # the size of the generated QR code is 1-40*21 (21 + (n-1) * 4) error_correction = qrcode. constants. ERROR_CORRECT_M, # L: 7% M: 15% Q: 25% H: 30% box_size = 10, # The pixel size of each grid is border = 2, # The grid width of the border) qr. add_data (str) qr. make (fit = True) img = qr. make_image () img. save (save) # generate a qr code image with a logo def make_logo_qr (str, logo, save): # configure the parameter qr = qrcode. QRCode (version = 4, error_correction = qrcode. constants. ERROR_CORRECT_Q, box_size = 8, border = 2) # Add the conversion content qr. add_data (str) # qr. make (fit = True) # generate qr code img = qr. make_image () # img = img. convert ("RGBA") # Add the logo if logo and OS. path. exists (logo): icon = Image. open (logo) # Get the size of the QR code image img_w, img_h = img. size factor = 4 size_w = int (img_w/factor) size_h = int (img_h/factor) # The size of the logo image cannot exceed 1/4 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) # Calculate the position of the logo in the QR code graph w = int (img_w-icon_w)/2) h = int (img_h-icon_h)/2) icon = icon. convert ("RGBA") img. paste (icon, (w, h), icon) # Save the processed image img. save (save) if _ name __= = '_ main __': save_path+'theqrcode.png '# Save the generated file logo='logo.jpg' # logo image str = raw_input ('Enter the text content of the QR code to be generated: ') # make_qr (str) make_logo_qr (str, logo, save_path)

How does Python use the QRCode module to generate a QR code?

1. Introduction
Python-qrcode is a third-party module used to generate QR code images. It depends on the PIL module and qrcode library.
2. Simple usage

import qrcode img = qrcode.make('hello, qrcode')img.save('test.png')

3. Advanced usage

import qrcode qr = qrcode.QRCode(     version=1,     error_correction=qrcode.constants.ERROR_CORRECT_L,     box_size=10,     border=4, ) qr.add_data('hello, qrcode') qr.make(fit=True) img = qr.make_image()img.save('123.png')

4. Parameter meaning:

Version: The value ranges from 1 ~ The integer of 40, which controls the size of the QR code (the minimum value is 1, which is a 12 × 12 matrix ). If you want the program to determine automatically, set the value to None and use the fit parameter.

Error_correction: controls the error correction function of the QR code. You can set the following four constants.
ERROR_CORRECT_L: errors of about 7% or fewer can be corrected.
ERROR_CORRECT_M (default): errors of about 15% or fewer can be corrected.
ROR_CORRECT_H: errors of about 30% or fewer can be corrected.

Box_size: controls the number of workers contained in each small grid in the QR code.

Border: number of cells contained in the control border (the distance between the QR code and the Image Boundary) (default value: 4, which is the minimum value specified by relevant standards)

I hope this article will help you learn Python programming.

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.