search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Install the qrcode Library
Create a Basic QR Code
Customize QR Code Appearance
Generate QR Code from User Input
Home Backend Development Python Tutorial How to generate QR codes in Python

How to generate QR codes in Python

Dec 27, 2025 am 05:56 AM

QR codes can be easily generated using the qrcode library. First install qrcode[pil], then create a QRCode instance, set the version, error correction level, box size and border, add data and generate images, support custom colors and user input, and finally save it as an image file.

How to generate QR codes in Python

To generate QR codes in Python, you can use the popular qrcode library. It's simple to use and highly customizable. Below is a step-by-step guide on how to create QR codes with different settings.

Install the qrcode Library

Before generating QR codes, install the qrcode package using pip:

pip install qrcode[pil]

The [pil] extra installs Pillow, which is needed to save the QR code as an image file.

Create a Basic QR Code

This example shows how to generate a simple QR code and save it as an image:

importqrcode

#Data to encode
data = "https://www.example.com"

#Create a QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)

# Add data to the QR code
qr.add_data(data)
qr.make(fit=True)

# Create an image from the QR code instance
img = qr.make_image(fill_color="black", back_color="white")

# Save the image
img.save("qrcode.png")

This generates a standard black-and-white QR code for a URL.

Customize QR Code Appearance

You can adjust several parameters to customize the QR code:

  • version : Controls size (1 to 40). Higher numbers mean larger codes.
  • error_correction : Options are ERROR_CORRECT_L , M , Q , H — higher levels allow more damage recovery.
  • box_size : Number of pixels per box.
  • border : Thickness of the border (minimum is 4).
  • fill_color and back_color : Change colors (requires Pillow).

Example with color:

img = qr.make_image(fill_color="blue", back_color="yellow")

Generate QR Code from User Input

You can make the script dynamic by taking input from the user:

data = input("Enter text or URL: ")
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("user_qr.png")

This lets users generate QR codes for any text or link.

Basically just pick your data, configure the QR code settings, and save it as an image. The library handles the encoding automatically.

The above is the detailed content of How to generate QR codes in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to perform data alignment in Python data analysis_Pandas index automatic alignment mechanism How to perform data alignment in Python data analysis_Pandas index automatic alignment mechanism Apr 17, 2026 am 11:21 AM

There are a lot of NaNs in df1 df2 because Pandas defaults to strict alignment by index (row name column name) instead of position; fill in NaN where there is no match; you can use .values ​​addition, add(fill_value=0) or align() to pre-align.

How to convert multiple lists into dictionaries in Python_Use zip function to build mapping relationship How to convert multiple lists into dictionaries in Python_Use zip function to build mapping relationship Apr 17, 2026 am 11:28 AM

Directly use dict(zip(keys,values)) to convert two equal-length lists into dictionaries. The keys elements are required to be hashable and non-duplicate. When the lengths are not equal, the zip is truncated and explicit verification is recommended. The key columns of the three lists need to be determined first, and then the values ​​are combined.

Why use requirements.in_with pip-tools to lock the version in Python Why use requirements.in_with pip-tools to lock the version in Python Apr 20, 2026 pm 05:54 PM

requirements.in only declares top-level dependencies and loose version constraints, and pip-compile automatically generates requirements.txt containing precise versions and hashes; the former is manually maintained and entered into Git, while the latter is machine-generated to ensure a consistent environment.

How Python handles massive training sets_Use MiniBatchKMeans for mini-batch clustering How Python handles massive training sets_Use MiniBatchKMeans for mini-batch clustering Apr 17, 2026 am 10:17 AM

MiniBatchKMeans is more suitable for massive data than KMeans because it only uses a small batch of samples (default 1024) to update the center each time, has stable memory and fast convergence; although it is an approximate solution, it is effective enough for tasks such as feature preprocessing.

How to batch modify the passwords of multiple hosts in Python with one click and generate reports How to batch modify the passwords of multiple hosts in Python with one click and generate reports Apr 17, 2026 am 11:24 AM

When using paramiko to change passwords in batches, you must first confirm that the target host supports SSH password modification. Passwd is stuck because pty is not allocated by default. You should use invoke_shell() to simulate terminal interaction, send passwords line by line, and handle prompts, errors, and special characters. At the same time, record detailed execution logs to troubleshoot problems.

How can Python completely solve the problem of garbled text and title display boxes in charts_Modify Matplotlib's built-in Chinese font configuration How can Python completely solve the problem of garbled text and title display boxes in charts_Modify Matplotlib's built-in Chinese font configuration Apr 16, 2026 am 10:33 AM

The root cause is that Matplotlib does not support Chinese fonts by default, the available Chinese font paths are not specified and the font cache is not cleared, causing the rendering to fall back to DejaVuSans without Chinese and display squares.

How to install TA-Lib financial indicator library in Python_solve compilation errors and underlying library dependencies How to install TA-Lib financial indicator library in Python_solve compilation errors and underlying library dependencies Apr 16, 2026 am 10:15 AM

Because TA-Lib is not a pure Python package and relies on the underlying C library, while PyPI only provides source code. Windows lacks the VC compilation environment, macOS lacks the Xcode tool chain, and Linux lacks build-essential and python3-dev, causing pipinstallTA-Lib to fail.

How Python solves the crash caused by the recursion depth limit_Using sys.setrecursionlimit How Python solves the crash caused by the recursion depth limit_Using sys.setrecursionlimit Apr 20, 2026 pm 05:51 PM

Directly increasing sys.setrecursionlimit() cannot really solve the recursive crash problem. Instead, it may cause a segmentation fault or memory exhaustion - it just moves the "explosion point" back and does not fix the explosive fuse.

Related articles