Qrcode PyPI
Qrcode PyPI
A standard install uses pypng to generate PNG files and can also
Download
render QR codes directly to the console. A standard install is just:
files
Statistics
pip install "qrcode[pil]"
GitHub statistics:
Stars: 4060 What is a QR Code?
Forks: 675
A Quick Response code is a two-dimensional pictographic code
Open issues: used for its fast readability and comparatively large storage
68 capacity. The code consists of black modules arranged in a square
pattern on a white background. The information encoded can be
Open PRs: 11
made up of any kind of data (e.g., binary, alphanumeric, or Kanji
View statistics for symbols)
this project via
Libraries.io , or by
using our public Usage
dataset on Google
BigQuery From the command line, use the installed qr script:
Author: Lincoln
Loop
import qrcode
img = qrcode.make('Some data here')
qr, denso-wave,
type(img) # qrcode.image.pil.PilImage
IEC18004
img.save("some_file.png")
Requires: Python
>=3.7
Advanced Usage
Maintainers
For more control, use the QRCode class. For example:
lincolnloop
import qrcode
qr = qrcode.QRCode(
maribedran
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
SmileyChris box_size=10,
border=4,
)
qr.add_data('Some data')
Classifiers qr.make(fit=True)
License painting color of the QR, when using the default image factory.
OSI Approved :: Both parameters accept RGB color tuples.
BSD License
Python :: 3.7
About 7% or less errors can be corrected.
Python :: 3.8
Python :: 3.9 ERROR_CORRECT_M (default)
Python :: 3.10
About 15% or less errors can be corrected.
Topic
Multimedia :: ERROR_CORRECT_Q
Graphics
Software About 25% or less errors can be corrected.
Development ::
ERROR_CORRECT_H .
Libraries :: Python
Modules About 30% or less errors can be corrected.
The border parameter controls how many boxes thick the border
should be (the default is 4, which is the minimum according to the
specs).
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.
Or in 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 o
factory = qrcode.image.svg.SvgPathImage
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:
import qrcode
qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgPath
qr.add_data('Some data')
qr.make(fit=True)
You can convert the SVG image into strings using the to_string()
method. Additional keyword arguments are forwarded to
ElementTrees tostring() :
img.to_string(encoding='unicode')
You can use the factory explicitly from your command line:
Or in Python:
import qrcode
from qrcode.image.pure import PyPNGImage
img = qrcode.make('Some data here', image_factory=PyPNGIm
Styled Image
Works only with versions >=7.2 (SVG styled images require 7.4).
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.
doc/module_drawers.png
doc/color_masks.png
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import Rounded
from qrcode.image.styles.colormasks import RadialGradiant
qr = qrcode.QRCode(error_correction=qrcode.constants.ERRO
qr.add_data('Some data')
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())
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()
Change log
7.4.2 (6 February 2023)
The pymaging image factory has been removed, but its factory
shortcut and the actual PymagingImage factory class now just
link to the PyPNGImage factory.
5.2.1
5.2.2
Revert some terminal changes from 5.2 that broke Python 3’s
real life tty code generation and introduce a better way from
Jacob Welsh.
Speed optimizations.
Change the output when using the qr script to use ASCII
rather than just colors, better using the terminal real estate.
Fix a bug in passing bytecode data directly when in Python 3.
Substation speed optimizations to best-fit algorithm (thanks
Jacob Welsh!).
Introduce a print_ascii method and use it as the default for
the qr script rather than print_tty .
5.0.1
4.0.1
4.0.2
4.0.3
4.0.4
3.1.1
Python 3 support.
Add QRCode.get_matrix, an easy way to get the matrix array of
a QR code including the border. Thanks Hugh Rawlinson.
Add in a workaround so that Python 2.6 users can use SVG
generation (they must install lxml ).
Some initial tests! And tox support ( pip install tox ) for
testing across Python platforms.
2.5.1
2.4.1
2.4.2
2.3.1
Developed and maintained by the Python community, for the Python community.
Donate today!
"PyPI", "Python Package Index", and the blocks logos are registered trademarks of the
Python Software Foundation .
English español français 日本語 português (Brasil) українська Ελληνικά Deutsch 中文 (简体)
中文 (繁體) русский עבריתesperanto
AWS Google
Datadog Fastly Microsoft
Cloud computing
and Security Download
Sponsor Monitoring CDN Analytics PSF Sponsor