Python QR Code image generator

Overview

Pure python QR Code generator

Generate QR codes.

For a standard install (which will include pillow for generating images), run:

pip install qrcode[pil]

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:

import qrcode
img = qrcode.make('Some data here')

Advanced Usage

For more control, use the QRCode class. For example:

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.

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:

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

Pure Python PNG

Install the following two packages:

pip install git+git://github.com/ojii/pymaging.git#egg=pymaging
pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

From your command line:

qr --factory=pymaging "Some text" > test.png

Or in Python:

import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make('Some data here', image_factory=PymagingImage)
Comments
  • ImportError: No module named qrcode

    ImportError: No module named qrcode

    after installing this module on a Raspberry Pi, I couldn't get the QR generation to work through the command line, nor through a .py script.

    It had previously worked with verion 5.1, and installing 5.1 over the 5.3 install solved my issue.

    opened by TurielD 21
  • State of the the project?

    State of the the project?

    Hi, this project looks like it is stalled. I see lots of issues and worth-to-merge pull requests. But no commits since 16 months. So, its a valuable project, sad if its abandoned.

    My questions:

    • What is the reason for dropped activity of the maintainers?
    • Is there an active fork I should know?
    • Or is there another project which is now the new standard I should know of?
    • If not, what can we do to make it move on? (like insert coin to continue, help to move to community driven project, ...)

    Thanks for any insights!

    opened by jensens 8
  • Is there a way to feed in a list of lists that contain 1s and 0s and create the QR code from that instead of a string?

    Is there a way to feed in a list of lists that contain 1s and 0s and create the QR code from that instead of a string?

    I have some symbols created from another library (it allows structured append), and I simply want to use python-qrcode to generate the image. Is there a way I can give qrcode.QRCode my list of lists of 1s and 0s so that it will take the binary data and turn it into an image with the normal version, error correction, box size, and border arguments?

    opened by ghost 8
  • Generated png is corrupted

    Generated png is corrupted

    It generates 435 bytes of png and it seems corrupted. I tried:

    qr "Some text" > test.png
    

    qrcode library version: 5.1 python: 2.7 PIL: 1.1.7 OS: Windows 7 64bit

    opened by digz6666 8
  • Support using either pymaging or PIL for tests

    Support using either pymaging or PIL for tests

    Instead of using Python version to determine whether pymaging is available or not, just try to import it. If it is not installed, try PIL as well. Then use either of them to run the PNG test or skip it appropriately.

    This makes it possible to run the tests without having to install pymaging that lacks PyPi releases.

    Additionally, I have added six dep to tox.ini since otherwise tests fail to run. You may want to remove either pillow or pymaging there since having both seems a bit pointless.

    opened by mgorny 8
  • Version Selection is wrong for certain string length

    Version Selection is wrong for certain string length

    Hi, I've noticed that the version is sometimes not selected correctly, depending on length of the string. ABCDEFGHIJKLMNO1 -> Version 1 (21x21) ABCDEFGHIJKLMNO12 -> Version1 (21x21) ABCDEFGHIJKLMNO123 -> Version2 (25x25) ABCDEFGHIJKLMNO1234 -> Version2 (25x25) ABCDEFGHIJKLMNO12345 -> Version1 (21x21) ABCDEFGHIJKLMNO123456 -> Version1 (21x21)

    Forcing to Level 1 (fit=False) gives the error: qrcode.exceptions.DataOverflowError: Code length overflow. Data size (156) > size available (152)

    opened by LXMK 7
  • QR Decoder Recommendation?

    QR Decoder Recommendation?

    Again apologize for posting here is there's a better place to ask a question.

    I'm REALLY liking this library you guys have built. It's working beautifully.

    I need a reader similar to your writer.

    Is there one you recommend?

    What do you use to test?

    If I can find something that matches up particularly well against python-qrcode then that seems the best way to go with my project.

    Thanks for all your efforts.

    opened by MikeTheWatchGuy 7
  • Generation of image file fails with Python 3

    Generation of image file fails with Python 3

    Generation of image file with Python 3 triggers TypeError: must be str, not bytes exception:

    $ PYTHONPATH="." python3.4 qrcode/console_scripts.py --help
    Usage: qr - Convert stdin (or the first argument) to a QR Code.
    
    When stdout is a tty the QR Code is printed to the terminal and when stdout is
    a pipe to a file an image is written. The default image format is PNG.
    ...
    $ PYTHONPATH="." python3.4 qrcode/console_scripts.py text
    █████████████████████████████
    █████████████████████████████
    ████ ▄▄▄▄▄ ███▄▄██ ▄▄▄▄▄ ████
    ████ █   █ █ ▀▄  █ █   █ ████
    ████ █▄▄▄█ █ ▄█▄ █ █▄▄▄█ ████
    ████▄▄▄▄▄▄▄█ █▄▀ █▄▄▄▄▄▄▄████
    ████ ▀▄ ▄ ▄██▄▀█ █▄ ▄  █▀████
    ████▄▀▄█ ▄▄▄▄█ ▄█▄█▀   ▄█████
    █████▄██▄█▄▄ ▀▄ ▀ ▀█▄▄█ ▀████
    ████ ▄▄▄▄▄ █▀▀ ▀ ▀ ▄█▀ ▄█████
    ████ █   █ █  ▄█ ██ █ ▀▄█████
    ████ █▄▄▄█ █▄ ▄▄█▄█▀ █ ██████
    ████▄▄▄▄▄▄▄█▄▄█▄█▄██▄█▄▄█████
    █████████████████████████████
    ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
    $ PYTHONPATH="." python3.4 qrcode/console_scripts.py text > /tmp/image.png
    Traceback (most recent call last):
      File "qrcode/console_scripts.py", line 65, in <module>
        main()
      File "qrcode/console_scripts.py", line 61, in main
        img.save(sys.stdout)
      File "/tmp/python-qrcode/qrcode/image/pil.py", line 32, in save
        self._img.save(stream, kind)
      File "/usr/lib64/python3.4/site-packages/PIL/Image.py", line 1685, in save
        save_handler(self, fp, filename)
      File "/usr/lib64/python3.4/site-packages/PIL/PngImagePlugin.py", line 631, in _save
        fp.write(_MAGIC)
    TypeError: must be str, not bytes
    

    Fix:

    --- qrcode/console_scripts.py
    +++ qrcode/console_scripts.py
    @@ -58,7 +58,7 @@
             return
    
         img = qr.make_image(image_factory=image_factory)
    -    img.save(sys.stdout)
    +    img.save(sys.stdout.buffer if sys.version_info[0] >= 3 else sys.stdout)
    
    
     if __name__ == "__main__":
    
    opened by Arfrever 7
  • Suggestion: Add title parameter

    Suggestion: Add title parameter

    When producing multiple QR codes it is very handy to include in the image some text to describe which one is which, currently the only way to do this is to add the title manually using an image/svg editor or with image magick.

    It would be great if there was a --title= parameter that allowed us to specify some text to have centred in the bottom border. An example is attached. okta_android

    opened by GadgetSteve 6
  • Reading of stdin with non-UTF-8 characters fails with Python 3

    Reading of stdin with non-UTF-8 characters fails with Python 3

    Reading of stdin with non-UTF-8 characters with Python 3 triggers UnicodeDecodeError exception:

    $ echo $'\x80' | PYTHONPATH="." python3.4 qrcode/console_scripts.py
    Traceback (most recent call last):
      File "qrcode/console_scripts.py", line 65, in <module>
        main()
      File "qrcode/console_scripts.py", line 50, in main
        data = sys.stdin.read()
      File "/usr/lib64/python3.4/codecs.py", line 313, in decode
        (result, consumed) = self._buffer_decode(data, self.errors, final)
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
    

    Fix:

    --- qrcode/console_scripts.py
    +++ qrcode/console_scripts.py
    @@ -47,7 +47,7 @@
         if args:
             data = args[0]
         else:
    -        data = sys.stdin.read()
    +        data = (sys.stdin.buffer if sys.version_info[0] >= 3 else sys.stdin).read()
         if opts.optimize is None:
             qr.add_data(data)
         else:
    --- qrcode/tests/test_script.py
    +++ qrcode/tests/test_script.py
    @@ -1,3 +1,4 @@
    +import sys
     try:
         import unittest2 as unittest
     except ImportError:
    @@ -30,7 +31,7 @@
             mock_stdin.configure_mock(**{'read.return_value': 'testtext'})
             with mock.patch('sys.stdin', mock_stdin) as stdin:
                 main([])
    -            self.assertTrue(stdin.read.called)
    +            self.assertTrue((stdin.buffer if sys.version_info[0] >= 3 else stdin).read.called)
             mock_print_ascii.assert_called_with(tty=True)
    
         @mock.patch('os.isatty', lambda *args: True)
    
    opened by Arfrever 6
  • Create QR-code images in Scalable Vector Graphics format

    Create QR-code images in Scalable Vector Graphics format

    These changes factor out QR-code image creation from the QRcode class to a separate image factory. PIL is still the default, but is no longer required. Two new renderers generate SVG images, one as an embeddable XML fragment and the other as a standalone XML document.

    For some applications, e.g., publishing and preprint, scalable images are more suitable than bitmaps.

    opened by brainy 6
  • python 3.9.1 setup uses legacy setup

    python 3.9.1 setup uses legacy setup

    For the install on python 3.9.1

    Python 3.9.1 (default, Dec  2 2022, 18:47:02) 
    [GCC 9.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    

    installing qrcode==7.3.1

    ubuntu@ip-172-31-11-201:$ pip install qrcode==7.3.1
    Collecting qrcode==7.3.1
      Using cached qrcode-7.3.1.tar.gz (43 kB)
      Preparing metadata (setup.py) ... done
    Installing collected packages: qrcode
      DEPRECATION: qrcode is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
      Running setup.py install for qrcode ... done
    Successfully installed qrcode-7.3.1
    
    opened by claytantor 0
  • Center Image is Not in Foreground

    Center Image is Not in Foreground

    Version

    python: 3.10.7 system: windows 11 qrcode: 7.3.1

    Issue

    I am using embedded_image_path to have an image in the center of the QRCode, but all it seems to be doing is putting the image in the background behind the fill_color. I would expect that when using embedded_image_path the image would be inside a square of unused space for the QRCode.

    opened by IRKillRoyDev 0
  • Is Image inheritance correct?

    Is Image inheritance correct?

    Version Information

    pythob: 3.9 system: windows 10 qrcode: 7.3.1

    My Issue

    I wanted to use the created qrcode PIL Image to use it in a different package class. Since this class checks for instance of type Image, the created Image of type qrcode.image.pil.PilImage does not match the PIL type Image and the application crashes.

    My Workaround

    At the moment I use the underscore_attribute image._img, which satisfies the isinstance check against Image. But I want to understand why you create your own version, which is not compatible with a normal Image type?

    opened by MaKaNu 1
  • Corrupt png generated via CLI on windows 10

    Corrupt png generated via CLI on windows 10

    Likely the same as #220 , though that user couldn't track this down. I also am not sure what to look for. Window 10, corporate issued laptop (some weirdness there?). Doing this from PowerShell.

    > qr.exe "test" > test.png
    

    ImageGlass, Windows Photos, Firefox, all give some sort of error about the file being unsupported or corrupt. Example from IG: image

    > python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    
    > pip freeze
    qrcode==7.3.1
    

    Let me know if there is any other information I can provide. I'll try later from my wife's windows computer and see if I can replicate. Attaching file for reference.

    test

    opened by jwhendy 0
  • Ability to use SVG as center image

    Ability to use SVG as center image

    Is there a way to use a svg file as the center image, and generate a scalable SVG that we could then use that final qr code? I can kind of get it to work with a png, but ends up being a low quality center image.

    Thanks!

    opened by misilot 0
Owner
Lincoln Loop
Makers of high performance web applications.
Lincoln Loop
A not exist person image generator python module

A not exist person image generator python module

Fayas Noushad 2 Dec 3, 2021
Combinatorial image generator for generative NFT art.

ImageGen Stitches multiple image layers together into one image. Run usage: stitch.py [-h] <backgrounds_dir> <dinos_dir> <traits_dir> <texture_file> <

Dinosols NFT 19 Sep 16, 2022
Panel Competition Image Generator

Panel Competition Image Generator This project was build by a member of the NFH community and is open for everyone who wants to try it. Relevant links

Juliano Mendieta 1 Oct 22, 2021
Archive of the image generator stuff from my API

alex_api_archive Archive of the image generator stuff from my API FAQ Q: Why? A: Because I am removing these components from the API Q: How do I run i

AlexFlipnote 26 Nov 17, 2022
Semi-hash-based Image Generator

pixel-planet Semi-hash-based Image Generator Utilizable for NFTs Generation Process Input is salted and hashed Colors (background, planet, stars) are

Bill Ni 3 Sep 1, 2022
Create a QR-code Generator app using only Python.

QR-code_Generator Create a QR-code Generator app using only Python. This apps generated a QR code for a single link. Libraryes used in this app --> py

Soham P Phasalkar 1 Oct 17, 2021
QR Code Generator

In this project, we'll be using some libraries to instantly generate authentic QR Codes and export them in various formats

Hassan Shahzad 3 Jun 2, 2022
Python avatar generator for absolute nerds

pagan Welcome to the Python Avatar Generator for Absolute Nerds. Current version: 0.4.3 View the change history here. Remember those good old days whe

David Bothe 280 Dec 16, 2022
Samila is a generative art generator written in Python

Samila is a generative art generator written in Python, Samila let's you create arts based on many thousand points. The position of every single point is calculated by a formula, which has random parameters. Because of the random numbers, every image looks different.

Sepand Haghighi 947 Dec 30, 2022
Python Digital Art Generator

Python Digital Art Generator The main goal of this repository is to generate all possible layers permutations given by the user in order to get unique

David Cuentas Mar 3 Mar 12, 2022
Avatar Generator Python

This is a simple avatar generator project which uses your webcam to take pictures and saves five different types of your images into your device including the original image.

Faisal Ahmed 3 Jan 23, 2022
Nanosensor Image Processor (NanoImgPro), a python-based image analysis tool for dopamine nanosensors

NanoImgPro Nanosensor Image Processor (NanoImgPro), a python-based image analysis tool for dopamine nanosensors NanoImgPro.py contains the main class

null 1 Mar 2, 2022
A pure python implementation of the GIMP XCF image format. Use this to interact with GIMP image formats

Pure Python implementation of the GIMP image formats (.xcf projects as well as brushes, patterns, etc)

FHPyhtonUtils 8 Dec 30, 2022
Image-Viewer is a Windows image viewer based on Python 3.

Image-Viewer Hi! Image-Viewer is a Windows image viewer based on Python 3. Using You must download Image-Viewer.exe from the root of the repository. T

null 2 Apr 18, 2022
Image Reading, Metadata Conversion, and Image Writing for Microscopy Images in Python

AICSImageIO Image Reading, Metadata Conversion, and Image Writing for Microscopy Images in Pure Python Features Supports reading metadata and imaging

Allen Institute for Cell Science - Modeling 137 Dec 14, 2022
Seaborn-image is a Python image visualization library based on matplotlib and provides a high-level API to draw attractive and informative images quickly and effectively.

seaborn-image: image data visualization Description Seaborn-image is a Python image visualization library based on matplotlib and provides a high-leve

null 48 Jan 5, 2023
A Python Script to convert Normal PNG Image to Apple iDOT PNG Image.

idot-png-encoder A Python Script to convert Normal PNG Image to Apple iDOT PNG Image (Multi-threaded Decoding PNG). Usage idotpngencoder.py -i <inputf

Lrdcq 2 Feb 17, 2022
Anaglyph 3D Converter - A python script that adds a 3D anaglyph style effect to an image using the Pillow image processing package.

Anaglyph 3D Converter - A python script that adds a 3D anaglyph style effect to an image using the Pillow image processing package.

Kizdude 2 Jan 22, 2022
Pyconvert is a python script that you can use to convert image files to another image format! (eg. PNG to ICO)

Pyconvert is a python script that you can use to convert image files to another image format! (eg. PNG to ICO)

null 1 Jan 16, 2022