search
HomeBackend DevelopmentPython TutorialHow to use Python to create a QR code? Share in multiple ways

How to use Python to create a QR code? The following article will share with you various methods of using Python to implement QR codes. If you need to use Python to create a QR code, you should bookmark this blog!

How to use Python to create a QR code? Share in multiple ways

As a qualified Python programmer, you will inevitably use QR code related operations in your work. So how can you quickly implement it in Python? Don’t worry, our blog will solve it for you.

It doesn’t matter if you don’t need it for the time being. Like and save it. After all, such a comprehensive article is not easy to find.

This article will bring you the four most common, simplest and most practical ways to generate QR codes in Python.

pyqrcode implements QR code

What I am showing you is a third-party QR code module. Open source and high efficiency are the mainstream in today's coding industry.

pyqrcode is a QR code generation module. After installation, you can create a simple QR code image with three lines of code.

pip install pyqrcode

After the module is installed, the introductory demo will be displayed directly. In order to generate images, an additional module needs to be installed, namely pip install pypng.

import pyqrcode
import png
qr = pyqrcode.create('梦想橡皮擦')
qr.png('ca.png', scale=5)

Run the code to directly generate a text QR code. Since some sites block the image content of the QR code, you can use your mobile phone to scan the code and test it yourself.

In addition to generating PNG images, you can also directly generate an svg image.

import pyqrcode
import png
qr = pyqrcode.create('梦想橡皮擦')
# qr.png('ca.png', scale=5)
qr.svg("./ca.svg", scale=8, background="white", module_color="#03a9f4")

There are no special explanations for the parameters in the above method. I believe you can learn and master them directly through the parameter names. Detailed parameter descriptions can also be found in the official manual and through a simple search.

import pyqrcode
import png
qr = pyqrcode.create('梦想橡皮擦')
# qr.png('ca.png', scale=5)
# qr.svg("./ca.svg", scale=8, background="white", module_color="#03a9f4")
qr.png('./code.png', scale=5, m

qrcode Implementation of QR code

The second recommended module has the same name as the previous module. It is also a relatively old third-party module, and this module is in actual combat The frequency of mid-term and exit is very high.

pip install qrcode

In order to learn quickly, check the sample code directly:

import qrcode
qr = qrcode.QRCode(
    version=None,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    border=4,
    box_size=10
)
img = qrcode.make('梦想橡皮擦',version=4,border=4,box_size=12)
img.save('ca.jpg')  # 保存图片

qrcode The most important method in the module is qrcode.QRCode, with simple parameters The description is as follows:

  • version: QR code size, ranging from 1-40;
  • error_correction: QR code error correction Range, the 4 constant values ​​are explained below;
  • box_size: The number of pixels contained in each small grid in the QR code;
  • border : Border, the distance between the QR code and the image border.

QR code error correction range, you can choose 4 constants:

  • ERROR_CORRECT_L: 7% or less of errors will be corrected;

  • ERROR_CORRECT_M (default value): Less than 15% of errors will be corrected;

  • ERROR_CORRECT_Q: Less than 25% of errors will be corrected;

  • ERROR_CORRECT_H: Less than 30% of errors will be corrected.

MyQR implements QR code

Another QR code generation module, the installation command is as follows:

pip install MyQR

This module is relatively young, you need to upgrade pillow to the latest version, use the following command:

pip install pillow>=8.3.2

First look at the ordinary QR code generation:

from MyQR import myqr

myqr.run(words="cacaca", version=9,
         save_name="ca1.png",
         save_dir="./")

A major feature of this QR code module is that it can achieve animated effects. The code is as follows:

from MyQR import myqr

myqr.run(words="https://juejin.cn/user/3966693684027512", version=3, picture="./in.gif", colorized=True,
         save_name="ca.gif",
         save_dir="./")

Animated images are also prepared for you.

run() The main parameters in the method are described as follows:

  • words: QR code text content (not supported Chinese);
  • picture: background picture;
  • colorsize: True, indicating generating color pictures;
  • save_name: The name of the QR code image.

Amazing-QR realizes QR code

can generate ordinary QR code, artistic QR code with pictures (black and white and color), dynamic QR code QR code (black and white and color).

The installation command is as follows:

pip install amzqr

The sample code is also very simple.

# 安装模块后
from amzqr import amzqr
import os
version, level, qr_name = amzqr.run(
    'cacaca',
    version=1,
    level='H',
    picture=None,
    colorized=False,
    contrast=1.0,
    brightness=1.0,
    save_name='ca_amzqr.png',
    save_dir=os.getcwd()
)

Chinese recognition is not very good and needs to be modified. If you think there are too many parameters, you can simplify it directly and only keep the words parameters.

from amzqr import amzqr
amzqr.run(words='https://juejin.cn/user/3966693684027512')

By default, a picture named qrcode.png is generated in the running directory.

【Related recommendations: Python3 video tutorial

The above is the detailed content of How to use Python to create a QR code? Share in multiple ways. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.