Top Related Projects
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.
A fast and compact QR Code encoding library
qr code generator
Cross-browser QRCode generator for javascript
QR Code Generator implementation in JavaScript, Java and more.
Quick Overview
The python-qrcode
library is a pure Python QR Code generator. It can be used to create QR codes as PNG images, SVG images, or in ASCII art. The library provides a simple and flexible API for generating QR codes with various customization options.
Pros
- Simple and Intuitive API: The library has a straightforward and easy-to-use API, making it accessible for developers of all skill levels.
- Customization Options: The library offers a wide range of customization options, allowing users to create QR codes with custom colors, sizes, and styles.
- Cross-platform Compatibility: The library is written in pure Python and can be used on various platforms, including Windows, macOS, and Linux.
- Active Development: The project is actively maintained, with regular updates and bug fixes.
Cons
- Limited Functionality: The library is focused on generating QR codes and does not provide additional features, such as scanning or decoding QR codes.
- Dependency on Pillow: The library requires the Pillow (PIL) library for image generation, which may be an additional dependency for some projects.
- Limited Error Handling: The library's error handling could be improved, as it may not provide detailed error messages in certain scenarios.
- Lack of Documentation: While the library has some documentation, it could be more comprehensive, especially for advanced use cases.
Code Examples
Here are a few examples of how to use the python-qrcode
library:
- Generate a basic QR code:
import qrcode
# Create a QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add the data to the QR code
qr.add_data('https://example.com')
qr.make(fit=True)
# Generate the QR code image
img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")
- Customize the QR code appearance:
import qrcode
from qrcode.constants import ERROR_CORRECT_H
# Create a QR code instance with custom settings
qr = qrcode.QRCode(
version=1,
error_correction=ERROR_CORRECT_H,
box_size=15,
border=5,
)
# Add the data to the QR code
qr.add_data('https://github.com/lincolnloop/python-qrcode')
qr.make(fit=True)
# Generate the QR code image with custom colors
img = qr.make_image(fill_color=(255, 0, 0), back_color=(255, 255, 255))
img.save("custom_qrcode.png")
- Generate a QR code with a logo:
import qrcode
from PIL import Image
# Create a QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add the data to the QR code
qr.add_data('https://github.com/lincolnloop/python-qrcode')
qr.make(fit=True)
# Generate the QR code image
img = qr.make_image(fill_color="black", back_color="white")
# Add a logo to the QR code
logo = Image.open("logo.png")
logo_size = 50
logo_img = logo.resize((logo_size, logo_size))
img.paste(logo_img, (int((img.size[0] - logo_size) / 2), int((img.size[1] - logo_size) / 2)))
img.save("qrcode_with_logo.png")
Getting Started
To get started with the python-qrcode
library, follow these steps:
- Install the library using pip:
pip
Competitor Comparisons
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
Pros of zxing
- Multi-format support: Handles various barcode formats beyond QR codes
- Cross-platform compatibility: Available for Java, Android, and other platforms
- Extensive documentation and community support
Cons of zxing
- Larger library size due to multi-format support
- Steeper learning curve for simple QR code tasks
- May be overkill for projects only requiring QR code functionality
Code Comparison
python-qrcode:
import qrcode
img = qrcode.make('https://example.com')
img.save('qr_code.png')
zxing:
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode("https://example.com", BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", Paths.get("qr_code.png"));
Summary
python-qrcode is a lightweight, Python-specific library focused solely on QR codes, making it ideal for simple QR code generation tasks. zxing, on the other hand, is a more comprehensive, multi-format barcode library with broader language and platform support. Choose python-qrcode for straightforward QR code projects in Python, and zxing for more complex, multi-format barcode needs across different platforms.
High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.
Pros of QR-Code-generator
- Multi-language support: Implementations in multiple programming languages (Java, TypeScript/JavaScript, Python, C++, C)
- Advanced features: Supports more QR code versions and error correction levels
- Performance: Generally faster and more efficient, especially for larger QR codes
Cons of QR-Code-generator
- Less user-friendly: Requires more setup and configuration compared to python-qrcode
- Limited built-in image output: Focuses on raw QR code data generation, requiring additional steps for image creation
Code Comparison
python-qrcode:
import qrcode
img = qrcode.make('https://example.com')
img.save('qr_code.png')
QR-Code-generator (Python):
import qrcodegen
qr = qrcodegen.QrCode.encode_text("https://example.com", qrcodegen.QrCode.Ecc.MEDIUM)
svg = qr.to_svg_str(4)
The python-qrcode example demonstrates its simplicity for basic usage, while the QR-Code-generator example shows more explicit control over QR code generation parameters. QR-Code-generator requires additional steps to save the output as an image file.
A fast and compact QR Code encoding library
Pros of libqrencode
- Written in C, offering high performance and efficiency
- Supports a wide range of output formats (PNG, EPS, SVG, ANSI, UTF8)
- Can be used as a library in various programming languages through bindings
Cons of libqrencode
- Requires compilation and installation, which can be complex for some users
- Less Python-friendly compared to python-qrcode
- May have a steeper learning curve for Python developers
Code Comparison
python-qrcode:
import qrcode
img = qrcode.make('Hello, World!')
img.save('qrcode.png')
libqrencode (using Python bindings):
import qrencode
(version, size, qr) = qrencode.encode('Hello, World!')
qrencode.write_png(qr, 'qrcode.png', size=6)
Summary
libqrencode is a powerful C library for generating QR codes, offering high performance and versatility. It supports multiple output formats and can be integrated into various programming languages. However, it may be less convenient for Python developers compared to python-qrcode, which is a native Python library. python-qrcode provides a simpler API and is easier to install and use in Python projects, but may not offer the same level of performance or output format options as libqrencode.
qr code generator
Pros of node-qrcode
- JavaScript-based, suitable for both Node.js and browser environments
- Supports various output formats (terminal, string, canvas, etc.)
- Extensive customization options for QR code appearance
Cons of node-qrcode
- Potentially slower performance compared to python-qrcode
- May have a steeper learning curve for Python developers
- Limited built-in error correction capabilities
Code Comparison
python-qrcode:
import qrcode
img = qrcode.make('Some data here')
img.save('qrcode.png')
node-qrcode:
const QRCode = require('qrcode')
QRCode.toFile('qrcode.png', 'Some data here', (err) => {
if (err) throw err
console.log('QR code saved!')
})
Both libraries offer simple ways to generate QR codes, but node-qrcode provides more flexibility in output formats and customization options. python-qrcode is more straightforward for basic use cases, while node-qrcode offers greater versatility for complex implementations.
The choice between these libraries largely depends on the developer's preferred language and the specific requirements of the project. python-qrcode may be more suitable for Python-based applications, while node-qrcode is ideal for JavaScript environments and web-based projects.
Cross-browser QRCode generator for javascript
Pros of qrcodejs
- Client-side generation: QR codes can be created directly in the browser without server requests
- Lightweight: Small file size and minimal dependencies
- Easy integration: Simple to include in web projects with just HTML and JavaScript
Cons of qrcodejs
- Limited functionality: Fewer options for customization compared to python-qrcode
- JavaScript-only: Not suitable for server-side or non-web applications
- Less actively maintained: Fewer recent updates and contributions
Code Comparison
python-qrcode:
import qrcode
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data('https://example.com')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save('qrcode.png')
qrcodejs:
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: "https://example.com",
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
Both libraries offer straightforward ways to generate QR codes, but python-qrcode provides more flexibility for server-side applications and image manipulation, while qrcodejs excels in simplicity for web-based projects.
QR Code Generator implementation in JavaScript, Java and more.
Pros of qrcode-generator
- Multi-language support: Implementations in JavaScript, Java, and other languages
- Wider range of output formats, including SVG and various image formats
- More customization options for QR code appearance and styling
Cons of qrcode-generator
- Less Python-specific functionality compared to python-qrcode
- May require more setup and configuration for Python users
- Documentation is not as comprehensive for Python implementation
Code Comparison
python-qrcode:
import qrcode
img = qrcode.make('https://example.com')
img.save('qr_code.png')
qrcode-generator (JavaScript example):
var qr = qrcode(0, 'M');
qr.addData('https://example.com');
qr.make();
var img = qr.createImgTag();
The python-qrcode library offers a more straightforward API for Python users, while qrcode-generator provides a similar functionality but requires slightly more setup. The qrcode-generator example is in JavaScript, as it's one of the primary supported languages.
Both libraries allow for easy QR code generation, but python-qrcode is more tailored for Python developers, while qrcode-generator offers broader language support and output options at the cost of some Python-specific convenience.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
============================= Pure python QR Code generator
Generate QR codes.
A standard install uses pypng_ to generate PNG files and can also render QR codes directly to the console. A standard install is just::
pip install qrcode
For more image functionality, install qrcode with the pil
dependency so
that pillow_ is installed and can be used for generating images::
pip install "qrcode[pil]"
.. _pypng: https://pypi.python.org/pypi/pypng .. _pillow: https://pypi.python.org/pypi/Pillow
What is a QR Code?
A Quick Response code is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or Kanji symbols)
Usage
From the command line, use the installed qr
script::
qr "Some text" > test.png
Or in Python, use the make
shortcut function:
.. code:: python
import qrcode
img = qrcode.make('Some data here')
type(img) # qrcode.image.pil.PilImage
img.save("some_file.png")
Advanced Usage
For more control, use the QRCode
class. For example:
.. code:: python
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
The version
parameter is an integer from 1 to 40 that controls the size of
the QR Code (the smallest, version 1, is a 21x21 matrix).
Set to None
and use the fit
parameter when making the code to determine
this automatically.
fill_color
and back_color
can change the background and the painting
color of the QR, when using the default image factory. Both parameters accept
RGB color tuples.
.. code:: python
img = qr.make_image(back_color=(255, 195, 235), fill_color=(55, 95, 35))
The error_correction
parameter controls the error correction used for the
QR Code. The following four constants are made available on the qrcode
package:
ERROR_CORRECT_L
About 7% or less errors can be corrected.
ERROR_CORRECT_M
(default)
About 15% or less errors can be corrected.
ERROR_CORRECT_Q
About 25% or less errors can be corrected.
ERROR_CORRECT_H
.
About 30% or less errors can be corrected.
The box_size
parameter controls how many pixels each "box" of the QR code
is.
The border
parameter controls how many boxes thick the border should be
(the default is 4, which is the minimum according to the specs).
Other image factories
You can encode as SVG, or use a new pure Python image processor to encode to PNG images.
The Python examples below use the make
shortcut. The same image_factory
keyword argument is a valid option for the QRCode
class for more advanced
usage.
SVG
You can create the entire SVG or an SVG fragment. When building an entire SVG image, you can use the factory that combines as a path (recommended, and default for the script) or a factory that creates a simple set of rectangles.
From your command line::
qr --factory=svg-path "Some text" > test.svg
qr --factory=svg "Some text" > test.svg
qr --factory=svg-fragment "Some text" > test.svg
Or in Python:
.. code:: python
import qrcode
import qrcode.image.svg
if method == 'basic':
# Simple factory, just a set of rects.
factory = qrcode.image.svg.SvgImage
elif method == 'fragment':
# Fragment factory (also just a set of rects)
factory = qrcode.image.svg.SvgFragmentImage
else:
# Combined path factory, fixes white space that may occur when zooming
factory = qrcode.image.svg.SvgPathImage
img = qrcode.make('Some data here', image_factory=factory)
Two other related factories are available that work the same, but also fill the background of the SVG with white::
qrcode.image.svg.SvgFillImage
qrcode.image.svg.SvgPathFillImage
The QRCode.make_image()
method forwards additional keyword arguments to the
underlying ElementTree XML library. This helps to fine tune the root element of
the resulting SVG:
.. code:: python
import qrcode
qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgPathImage)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(attrib={'class': 'some-css-class'})
You can convert the SVG image into strings using the to_string()
method.
Additional keyword arguments are forwarded to ElementTrees tostring()
:
.. code:: python
img.to_string(encoding='unicode')
Pure Python PNG
If Pillow is not installed, the default image factory will be a pure Python PNG
encoder that uses pypng
.
You can use the factory explicitly from your command line::
qr --factory=png "Some text" > test.png
Or in Python:
.. code:: python
import qrcode
from qrcode.image.pure import PyPNGImage
img = qrcode.make('Some data here', image_factory=PyPNGImage)
Styled Image
Works only with versions_ >=7.2 (SVG styled images require 7.4).
.. _versions: https://github.com/lincolnloop/python-qrcode/blob/master/CHANGES.rst#72-19-july-2021
To apply styles to the QRCode, use the StyledPilImage
or one of the
standard SVG_ image factories. These accept an optional module_drawer
parameter to control the shape of the QR Code.
These QR Codes are not guaranteed to work with all readers, so do some experimentation and set the error correction to high (especially if embedding an image).
Other PIL module drawers:
.. image:: doc/module_drawers.png
For SVGs, use SvgSquareDrawer
, SvgCircleDrawer
,
SvgPathSquareDrawer
, or SvgPathCircleDrawer
.
These all accept a size_ratio
argument which allows for "gapped" squares or
circles by reducing this less than the default of Decimal(1)
.
The StyledPilImage
additionally accepts an optional color_mask
parameter to change the colors of the QR Code, and an optional
embeded_image_path
to embed an image in the center of the code.
Other color masks:
.. image:: doc/color_masks.png
Here is a code example to draw a QR code with rounded corners, radial gradient and an embedded image:
.. code:: python
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
from qrcode.image.styles.colormasks import RadialGradiantColorMask
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_L)
qr.add_data('Some data')
img_1 = qr.make_image(image_factory=StyledPilImage, module_drawer=RoundedModuleDrawer())
img_2 = qr.make_image(image_factory=StyledPilImage, color_mask=RadialGradiantColorMask())
img_3 = qr.make_image(image_factory=StyledPilImage, embeded_image_path="/path/to/image.png")
Examples
Get the text content from print_ascii
:
.. code:: python
import io
import qrcode
qr = qrcode.QRCode()
qr.add_data("Some text")
f = io.StringIO()
qr.print_ascii(out=f)
f.seek(0)
print(f.read())
The add_data
method will append data to the current QR object. To add new data by replacing previous content in the same object, first use clear method:
.. code:: python
import qrcode
qr = qrcode.QRCode()
qr.add_data('Some data')
img = qr.make_image()
qr.clear()
qr.add_data('New data')
other_img = qr.make_image()
Pipe ascii output to text file in command line::
qr --ascii "Some data" > "test.txt"
cat test.txt
Alternative to piping output to file to avoid PowerShell issues::
# qr "Some data" > test.png
qr --output=test.png "Some data"
Top Related Projects
ZXing ("Zebra Crossing") barcode scanning library for Java, Android
High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.
A fast and compact QR Code encoding library
qr code generator
Cross-browser QRCode generator for javascript
QR Code Generator implementation in JavaScript, Java and more.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot