qrref 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qrref-0.1.0/.gitignore +218 -0
- qrref-0.1.0/LICENSE +21 -0
- qrref-0.1.0/PKG-INFO +117 -0
- qrref-0.1.0/README.md +100 -0
- qrref-0.1.0/pyproject.toml +29 -0
- qrref-0.1.0/qrref/__init__.py +8 -0
- qrref-0.1.0/qrref/__main__.py +76 -0
- qrref-0.1.0/qrref/custom_types.py +51 -0
- qrref-0.1.0/qrref/data_analysis.py +168 -0
- qrref-0.1.0/qrref/data_encoding.py +156 -0
- qrref-0.1.0/qrref/error_correction.py +181 -0
- qrref-0.1.0/qrref/galois_field.py +74 -0
- qrref-0.1.0/qrref/masking.py +119 -0
- qrref-0.1.0/qrref/placement.py +213 -0
- qrref-0.1.0/qrref/qr.py +121 -0
- qrref-0.1.0/qrref/settings.py +27 -0
- qrref-0.1.0/qrref/table_data.py +225 -0
- qrref-0.1.0/sample_qr_code.png +0 -0
- qrref-0.1.0/tests/test_data_analysis.py +32 -0
- qrref-0.1.0/tests/test_data_encoding.py +87 -0
- qrref-0.1.0/tests/test_error_correction.py +169 -0
- qrref-0.1.0/tests/test_galois_field.py +34 -0
- qrref-0.1.0/tests/test_masking.py +70 -0
- qrref-0.1.0/tests/test_placement.py +91 -0
- qrref-0.1.0/tests/test_table_data.py +70 -0
qrref-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
*.pdf
|
|
2
|
+
|
|
3
|
+
# Byte-compiled / optimized / DLL files
|
|
4
|
+
__pycache__/
|
|
5
|
+
*.py[codz]
|
|
6
|
+
*$py.class
|
|
7
|
+
|
|
8
|
+
# C extensions
|
|
9
|
+
*.so
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.Python
|
|
13
|
+
build/
|
|
14
|
+
develop-eggs/
|
|
15
|
+
dist/
|
|
16
|
+
downloads/
|
|
17
|
+
eggs/
|
|
18
|
+
.eggs/
|
|
19
|
+
lib/
|
|
20
|
+
lib64/
|
|
21
|
+
parts/
|
|
22
|
+
sdist/
|
|
23
|
+
var/
|
|
24
|
+
wheels/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
# Usually these files are written by a python script from a template
|
|
33
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
34
|
+
*.manifest
|
|
35
|
+
*.spec
|
|
36
|
+
|
|
37
|
+
# Installer logs
|
|
38
|
+
pip-log.txt
|
|
39
|
+
pip-delete-this-directory.txt
|
|
40
|
+
|
|
41
|
+
# Unit test / coverage reports
|
|
42
|
+
htmlcov/
|
|
43
|
+
.tox/
|
|
44
|
+
.nox/
|
|
45
|
+
.coverage
|
|
46
|
+
.coverage.*
|
|
47
|
+
.cache
|
|
48
|
+
nosetests.xml
|
|
49
|
+
coverage.xml
|
|
50
|
+
*.cover
|
|
51
|
+
*.py.cover
|
|
52
|
+
.hypothesis/
|
|
53
|
+
.pytest_cache/
|
|
54
|
+
cover/
|
|
55
|
+
|
|
56
|
+
# Translations
|
|
57
|
+
*.mo
|
|
58
|
+
*.pot
|
|
59
|
+
|
|
60
|
+
# Django stuff:
|
|
61
|
+
*.log
|
|
62
|
+
local_settings.py
|
|
63
|
+
db.sqlite3
|
|
64
|
+
db.sqlite3-journal
|
|
65
|
+
|
|
66
|
+
# Flask stuff:
|
|
67
|
+
instance/
|
|
68
|
+
.webassets-cache
|
|
69
|
+
|
|
70
|
+
# Scrapy stuff:
|
|
71
|
+
.scrapy
|
|
72
|
+
|
|
73
|
+
# Sphinx documentation
|
|
74
|
+
docs/_build/
|
|
75
|
+
|
|
76
|
+
# PyBuilder
|
|
77
|
+
.pybuilder/
|
|
78
|
+
target/
|
|
79
|
+
|
|
80
|
+
# Jupyter Notebook
|
|
81
|
+
.ipynb_checkpoints
|
|
82
|
+
|
|
83
|
+
# IPython
|
|
84
|
+
profile_default/
|
|
85
|
+
ipython_config.py
|
|
86
|
+
|
|
87
|
+
# pyenv
|
|
88
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
89
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
90
|
+
# .python-version
|
|
91
|
+
|
|
92
|
+
# pipenv
|
|
93
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
94
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
95
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
96
|
+
# install all needed dependencies.
|
|
97
|
+
# Pipfile.lock
|
|
98
|
+
|
|
99
|
+
# UV
|
|
100
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
101
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
102
|
+
# commonly ignored for libraries.
|
|
103
|
+
# uv.lock
|
|
104
|
+
|
|
105
|
+
# poetry
|
|
106
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
107
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
108
|
+
# commonly ignored for libraries.
|
|
109
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
110
|
+
# poetry.lock
|
|
111
|
+
# poetry.toml
|
|
112
|
+
|
|
113
|
+
# pdm
|
|
114
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
115
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
116
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
117
|
+
# pdm.lock
|
|
118
|
+
# pdm.toml
|
|
119
|
+
.pdm-python
|
|
120
|
+
.pdm-build/
|
|
121
|
+
|
|
122
|
+
# pixi
|
|
123
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
124
|
+
# pixi.lock
|
|
125
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
126
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
127
|
+
.pixi
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
201
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
203
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
.vscode/
|
|
205
|
+
|
|
206
|
+
# Ruff stuff:
|
|
207
|
+
.ruff_cache/
|
|
208
|
+
|
|
209
|
+
# PyPI configuration file
|
|
210
|
+
.pypirc
|
|
211
|
+
|
|
212
|
+
# Marimo
|
|
213
|
+
marimo/_static/
|
|
214
|
+
marimo/_lsp/
|
|
215
|
+
__marimo__/
|
|
216
|
+
|
|
217
|
+
# Streamlit
|
|
218
|
+
.streamlit/secrets.toml
|
qrref-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Truls Henriksson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
qrref-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: qrref
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: QR code generator with a command-line interface for showing, saving and copying generated QR codes.
|
|
5
|
+
Project-URL: Homepage, https://github.com/TrulsHenriksson/qrref/
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Operating System :: OS Independent
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Requires-Dist: matplotlib
|
|
12
|
+
Requires-Dist: numpy
|
|
13
|
+
Requires-Dist: pillow
|
|
14
|
+
Provides-Extra: copy
|
|
15
|
+
Requires-Dist: pyperclipimg; extra == 'copy'
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# qrref
|
|
19
|
+
|
|
20
|
+
Python package and reference implementation of the ISO/IEC 18004:2015 Standard for QR codes, with a simple but convenient CLI for showing, saving and copying generated QR codes.
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+
|
|
24
|
+
# Purpose
|
|
25
|
+
|
|
26
|
+
This is first and foremost a reference implementation, meaning I wanted the code to be readable and well-documented, without lots of magic numbers and tables copied straight from the specification. For those wanting to implement their own QR code generator, or extending one to suit their needs (custom colors, styles, inset images, etc.) this should be a good basis.
|
|
27
|
+
|
|
28
|
+
Wherever possible, I've chosen to implement the table data as functions instead. This doesn't result in the shortest or fastest implementation, but should explain where the numbers come from better.
|
|
29
|
+
|
|
30
|
+
The priorities of this project have been as follows, in approximate order of importance.
|
|
31
|
+
1. **Referentiability.** The code should adhere closely to the specification.
|
|
32
|
+
- Most functions and tables are accompanied by comments referencing where they can be found in the specification. Look up "ISO/IEC 18004" and you can probably find it somewhere.
|
|
33
|
+
2. **Clarity.** The code should be easy to read and understand.
|
|
34
|
+
- Comments and docstrings are mandatory.
|
|
35
|
+
- The code is split up into files with clearly different responsibilities, and should be understandable (mostly) on their own.
|
|
36
|
+
3. **Transparency.** Table data should be generated by functions wherever possible.
|
|
37
|
+
- The implementation could be made shorter by hard-coding certain parts, most notably the coefficients of the generating polynomials for the Solomon-Reed code. This is against the spirit of this project though.
|
|
38
|
+
- Table data is still preferred where a function would be less clear.
|
|
39
|
+
4. **Efficiency.** Looping over the entire symbol in Python should be avoided.
|
|
40
|
+
- NumPy is used extensively to speed up generation.
|
|
41
|
+
|
|
42
|
+
# Usage
|
|
43
|
+
|
|
44
|
+
Install by running
|
|
45
|
+
```bash
|
|
46
|
+
$ pip install qrref
|
|
47
|
+
```
|
|
48
|
+
or to also be able to copy QR codes directly to the clipboard, by running
|
|
49
|
+
```bash
|
|
50
|
+
$ pip install qrref[copy]
|
|
51
|
+
```
|
|
52
|
+
This is an optional dependency, since it uses `pyperclipimg` which states in its README: "This module is separate [from pyperclip] because it has some heavy dependencies (pywin32, Quartz, etc.)" Without it, you can still show and save QR codes as png.
|
|
53
|
+
|
|
54
|
+
You can then `import qrref` and use `qrref.generate_qr_code` in your own code, or use the command-line interface (CLI) directly.
|
|
55
|
+
|
|
56
|
+
## Examples
|
|
57
|
+
|
|
58
|
+
Show a QR code with the message "This is a QR code". This is done using Matplotlib, which means that "F" toggles fullscreen and "Q" closes the window. Perfect for quickly throwing up a QR code on a big screen.
|
|
59
|
+
```bash
|
|
60
|
+
$ py -m qrref "This is a QR code"
|
|
61
|
+
```
|
|
62
|
+
Save the QR code to `./wikipedia_qr.png`:
|
|
63
|
+
```bash
|
|
64
|
+
$ py -m qrref "https://wikipedia.org" --png --filename wikipedia_qr
|
|
65
|
+
```
|
|
66
|
+
Copy the QR code to the clipboard (requires installing with `pip install qrref[copy]`):
|
|
67
|
+
```bash
|
|
68
|
+
$ py -m qrref "https://wikipedia.org" --copy
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
# Reference details
|
|
72
|
+
|
|
73
|
+
The generation of a QR code is handled by `qrref.qr.generate_qr_code`. This function is fairly readable and illustrates the process from start to finish:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
def generate_qr_code(
|
|
77
|
+
content: str,
|
|
78
|
+
ec_level: Literal["L", "M", "Q", "H"],
|
|
79
|
+
version: int | None = None,
|
|
80
|
+
):
|
|
81
|
+
if version is None:
|
|
82
|
+
version, modes = select_version(content, ec_level)
|
|
83
|
+
else:
|
|
84
|
+
modes = select_modes(content, version)
|
|
85
|
+
|
|
86
|
+
bitstream = encode_mixed(modes, version)
|
|
87
|
+
bytestream = to_data_bytestream(bitstream, version, ec_level)
|
|
88
|
+
blockstream = generate_error_correction_blocks(bytestream, version, ec_level)
|
|
89
|
+
final_bytestream = interleave_blocks(blockstream, version, ec_level)
|
|
90
|
+
|
|
91
|
+
symbol = place_bytestream(final_bytestream, version)
|
|
92
|
+
insert_timing_patterns(symbol)
|
|
93
|
+
insert_finder_patterns(symbol)
|
|
94
|
+
insert_alignment_patterns(symbol, version)
|
|
95
|
+
|
|
96
|
+
symbol, mask_pattern_id = apply_mask(symbol, version)
|
|
97
|
+
|
|
98
|
+
place_format_bits(symbol, generate_format_bits(ec_level, mask_pattern_id))
|
|
99
|
+
if version >= 7:
|
|
100
|
+
place_version_bits(symbol, generate_version_bits(version))
|
|
101
|
+
|
|
102
|
+
symbol = expand_quiet_region(symbol)
|
|
103
|
+
return symbol
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
This uses functions split up over several files. Here's a high-level overview of what each file does, in the order they are used in the above function.
|
|
107
|
+
1. `qrref/data_analysis.py`: Select the minimum version (qr code size) that the content (input string) fits in. Also select the encoding types that minimizes the length of the data bitstream (numeric data is denser than alphanumeric data, which is denser than byte data).
|
|
108
|
+
2. `qrref/data_encoding.py`: Encode the message into a bitstream and group the bits together into bytes.
|
|
109
|
+
3. `qrref/error_correction.py`: Generate error correction bytes using the Reed-Solomon codes. In practice, does this by finding polynomial remainders over the Galois Field of order 256 ([wikipedia](https://en.wikipedia.org/wiki/Finite_field_arithmetic)), which also uses `qrref/galois_field.py`. Turn the resulting data and error correction bytes into a bitstream by splitting them into blocks and interleaving those blocks. Also generate the format and version bits after the masking is complete.
|
|
110
|
+
4. `qrref/placement.py`: Place the finalized bitstream into the symbol, as well as finder, alignment and timing patterns. After the format and version bits are generated, place those too.
|
|
111
|
+
5. `qrref/masking.py`: Find and apply the mask that minimizes undesirable patterns, like long runs of the same color, etc. This is done after placing everything but the format and version bits in the symbol.
|
|
112
|
+
|
|
113
|
+
Auxiliary files:
|
|
114
|
+
1. `qrref/custom_types.py`: Custom types for type hinting throughout the package.
|
|
115
|
+
2. `qrref/qr.py`: Assemble and visualize the QR code.
|
|
116
|
+
3. `qrref/settings.py`: Global settings. Currently only one, which is the encoding to use for byte strings (Latin-1 or UTF-8).
|
|
117
|
+
4. `qrref/table_data.py`: Data from tables, either in the form of dictionaries or functions.
|
qrref-0.1.0/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# qrref
|
|
2
|
+
|
|
3
|
+
Python package and reference implementation of the ISO/IEC 18004:2015 Standard for QR codes, with a simple but convenient CLI for showing, saving and copying generated QR codes.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
# Purpose
|
|
8
|
+
|
|
9
|
+
This is first and foremost a reference implementation, meaning I wanted the code to be readable and well-documented, without lots of magic numbers and tables copied straight from the specification. For those wanting to implement their own QR code generator, or extending one to suit their needs (custom colors, styles, inset images, etc.) this should be a good basis.
|
|
10
|
+
|
|
11
|
+
Wherever possible, I've chosen to implement the table data as functions instead. This doesn't result in the shortest or fastest implementation, but should explain where the numbers come from better.
|
|
12
|
+
|
|
13
|
+
The priorities of this project have been as follows, in approximate order of importance.
|
|
14
|
+
1. **Referentiability.** The code should adhere closely to the specification.
|
|
15
|
+
- Most functions and tables are accompanied by comments referencing where they can be found in the specification. Look up "ISO/IEC 18004" and you can probably find it somewhere.
|
|
16
|
+
2. **Clarity.** The code should be easy to read and understand.
|
|
17
|
+
- Comments and docstrings are mandatory.
|
|
18
|
+
- The code is split up into files with clearly different responsibilities, and should be understandable (mostly) on their own.
|
|
19
|
+
3. **Transparency.** Table data should be generated by functions wherever possible.
|
|
20
|
+
- The implementation could be made shorter by hard-coding certain parts, most notably the coefficients of the generating polynomials for the Solomon-Reed code. This is against the spirit of this project though.
|
|
21
|
+
- Table data is still preferred where a function would be less clear.
|
|
22
|
+
4. **Efficiency.** Looping over the entire symbol in Python should be avoided.
|
|
23
|
+
- NumPy is used extensively to speed up generation.
|
|
24
|
+
|
|
25
|
+
# Usage
|
|
26
|
+
|
|
27
|
+
Install by running
|
|
28
|
+
```bash
|
|
29
|
+
$ pip install qrref
|
|
30
|
+
```
|
|
31
|
+
or to also be able to copy QR codes directly to the clipboard, by running
|
|
32
|
+
```bash
|
|
33
|
+
$ pip install qrref[copy]
|
|
34
|
+
```
|
|
35
|
+
This is an optional dependency, since it uses `pyperclipimg` which states in its README: "This module is separate [from pyperclip] because it has some heavy dependencies (pywin32, Quartz, etc.)" Without it, you can still show and save QR codes as png.
|
|
36
|
+
|
|
37
|
+
You can then `import qrref` and use `qrref.generate_qr_code` in your own code, or use the command-line interface (CLI) directly.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
Show a QR code with the message "This is a QR code". This is done using Matplotlib, which means that "F" toggles fullscreen and "Q" closes the window. Perfect for quickly throwing up a QR code on a big screen.
|
|
42
|
+
```bash
|
|
43
|
+
$ py -m qrref "This is a QR code"
|
|
44
|
+
```
|
|
45
|
+
Save the QR code to `./wikipedia_qr.png`:
|
|
46
|
+
```bash
|
|
47
|
+
$ py -m qrref "https://wikipedia.org" --png --filename wikipedia_qr
|
|
48
|
+
```
|
|
49
|
+
Copy the QR code to the clipboard (requires installing with `pip install qrref[copy]`):
|
|
50
|
+
```bash
|
|
51
|
+
$ py -m qrref "https://wikipedia.org" --copy
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
# Reference details
|
|
55
|
+
|
|
56
|
+
The generation of a QR code is handled by `qrref.qr.generate_qr_code`. This function is fairly readable and illustrates the process from start to finish:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
def generate_qr_code(
|
|
60
|
+
content: str,
|
|
61
|
+
ec_level: Literal["L", "M", "Q", "H"],
|
|
62
|
+
version: int | None = None,
|
|
63
|
+
):
|
|
64
|
+
if version is None:
|
|
65
|
+
version, modes = select_version(content, ec_level)
|
|
66
|
+
else:
|
|
67
|
+
modes = select_modes(content, version)
|
|
68
|
+
|
|
69
|
+
bitstream = encode_mixed(modes, version)
|
|
70
|
+
bytestream = to_data_bytestream(bitstream, version, ec_level)
|
|
71
|
+
blockstream = generate_error_correction_blocks(bytestream, version, ec_level)
|
|
72
|
+
final_bytestream = interleave_blocks(blockstream, version, ec_level)
|
|
73
|
+
|
|
74
|
+
symbol = place_bytestream(final_bytestream, version)
|
|
75
|
+
insert_timing_patterns(symbol)
|
|
76
|
+
insert_finder_patterns(symbol)
|
|
77
|
+
insert_alignment_patterns(symbol, version)
|
|
78
|
+
|
|
79
|
+
symbol, mask_pattern_id = apply_mask(symbol, version)
|
|
80
|
+
|
|
81
|
+
place_format_bits(symbol, generate_format_bits(ec_level, mask_pattern_id))
|
|
82
|
+
if version >= 7:
|
|
83
|
+
place_version_bits(symbol, generate_version_bits(version))
|
|
84
|
+
|
|
85
|
+
symbol = expand_quiet_region(symbol)
|
|
86
|
+
return symbol
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
This uses functions split up over several files. Here's a high-level overview of what each file does, in the order they are used in the above function.
|
|
90
|
+
1. `qrref/data_analysis.py`: Select the minimum version (qr code size) that the content (input string) fits in. Also select the encoding types that minimizes the length of the data bitstream (numeric data is denser than alphanumeric data, which is denser than byte data).
|
|
91
|
+
2. `qrref/data_encoding.py`: Encode the message into a bitstream and group the bits together into bytes.
|
|
92
|
+
3. `qrref/error_correction.py`: Generate error correction bytes using the Reed-Solomon codes. In practice, does this by finding polynomial remainders over the Galois Field of order 256 ([wikipedia](https://en.wikipedia.org/wiki/Finite_field_arithmetic)), which also uses `qrref/galois_field.py`. Turn the resulting data and error correction bytes into a bitstream by splitting them into blocks and interleaving those blocks. Also generate the format and version bits after the masking is complete.
|
|
93
|
+
4. `qrref/placement.py`: Place the finalized bitstream into the symbol, as well as finder, alignment and timing patterns. After the format and version bits are generated, place those too.
|
|
94
|
+
5. `qrref/masking.py`: Find and apply the mask that minimizes undesirable patterns, like long runs of the same color, etc. This is done after placing everything but the format and version bits in the symbol.
|
|
95
|
+
|
|
96
|
+
Auxiliary files:
|
|
97
|
+
1. `qrref/custom_types.py`: Custom types for type hinting throughout the package.
|
|
98
|
+
2. `qrref/qr.py`: Assemble and visualize the QR code.
|
|
99
|
+
3. `qrref/settings.py`: Global settings. Currently only one, which is the encoding to use for byte strings (Latin-1 or UTF-8).
|
|
100
|
+
4. `qrref/table_data.py`: Data from tables, either in the form of dictionaries or functions.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "qrref"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "QR code generator with a command-line interface for showing, saving and copying generated QR codes."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"numpy",
|
|
13
|
+
"matplotlib",
|
|
14
|
+
"pillow",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
license = "MIT"
|
|
21
|
+
license-files = ["LICEN[CS]E*"]
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/TrulsHenriksson/qrref/"
|
|
25
|
+
|
|
26
|
+
[project.optional-dependencies]
|
|
27
|
+
copy = [
|
|
28
|
+
"pyperclipimg"
|
|
29
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
|
|
3
|
+
from qrref.settings import SETTINGS
|
|
4
|
+
from qrref.qr import (
|
|
5
|
+
generate_qr_code,
|
|
6
|
+
show_qr_code,
|
|
7
|
+
save_qr_code,
|
|
8
|
+
copy_qr_code,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Create the parser
|
|
13
|
+
parser = argparse.ArgumentParser(description="Create a QR code and save, show or copy it.")
|
|
14
|
+
parser.add_argument("content", nargs="?", help="What to encode in the QR code", default="https://youtu.be/NR1TdhpCfcY")
|
|
15
|
+
|
|
16
|
+
ec_levels = parser.add_argument_group("Error correction level").add_mutually_exclusive_group()
|
|
17
|
+
ec_levels.add_argument("-L", action="store_true", help="Low: 7 percent error correction")
|
|
18
|
+
ec_levels.add_argument("-M", action="store_true", help="Medium: 15 percent error correction (default)")
|
|
19
|
+
ec_levels.add_argument("-Q", action="store_true", help="Quartile: 25 percent error correction")
|
|
20
|
+
ec_levels.add_argument("-H", action="store_true", help="High: 30 percent error correction")
|
|
21
|
+
|
|
22
|
+
parser.add_argument(
|
|
23
|
+
"-v",
|
|
24
|
+
"--version",
|
|
25
|
+
type=int,
|
|
26
|
+
default=0,
|
|
27
|
+
help=(
|
|
28
|
+
"Which version (size) of QR code to use. By default, chooses the smallest one"
|
|
29
|
+
" that the content fits in."
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
formats = parser.add_argument_group("How to save the QR code").add_mutually_exclusive_group()
|
|
34
|
+
formats.add_argument("--png", action="store_true", help="As PNG")
|
|
35
|
+
formats.add_argument("--show", action="store_true", help="Show without saving (default)")
|
|
36
|
+
formats.add_argument("--copy", action="store_true", help="Copy image to clipboard")
|
|
37
|
+
|
|
38
|
+
parser.add_argument("-f", "--filename", help="Filename to save with", default="qr_code")
|
|
39
|
+
parser.add_argument("-t", "--transparent", action="store_true", help="Use transparent background")
|
|
40
|
+
parser.add_argument("-u", "--utf-8" , action="store_true", help="Use UTF-8 instead of Latin-1 encoding")
|
|
41
|
+
parser.add_argument("--debug", action="store_true", help="Show debug output")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
args = parser.parse_args()
|
|
46
|
+
if args.utf_8:
|
|
47
|
+
SETTINGS.byte_encoding = "utf-8"
|
|
48
|
+
|
|
49
|
+
# Find the selected ec_level, otherwise use "M"
|
|
50
|
+
for ec_level, flag in zip(("L", "M", "Q", "H"), (args.L, args.M, args.Q, args.H)):
|
|
51
|
+
if flag:
|
|
52
|
+
break
|
|
53
|
+
else:
|
|
54
|
+
ec_level = "M"
|
|
55
|
+
|
|
56
|
+
# Find the selected format, otherwise use "show"
|
|
57
|
+
for format, flag in zip(("png", "show", "copy"), (args.png, args.show, args.copy)):
|
|
58
|
+
if flag:
|
|
59
|
+
break
|
|
60
|
+
else:
|
|
61
|
+
format = "show"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
version = args.version if 1 <= args.version <= 40 else None
|
|
65
|
+
symbol = generate_qr_code(args.content, ec_level, version, debug=args.debug)
|
|
66
|
+
|
|
67
|
+
match format:
|
|
68
|
+
case "show":
|
|
69
|
+
show_qr_code(symbol)
|
|
70
|
+
case "png":
|
|
71
|
+
file_path = args.filename + "." + format
|
|
72
|
+
save_qr_code(symbol, file_path, args.transparent)
|
|
73
|
+
print(f"QR code successfully saved to {file_path}.")
|
|
74
|
+
case "copy":
|
|
75
|
+
copy_qr_code(symbol, args.transparent)
|
|
76
|
+
print("QR code succesfully copied to clipboard.")
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from typing import Generator, Sequence, Literal, Iterable, Callable, Protocol
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Bit",
|
|
8
|
+
"Byte",
|
|
9
|
+
"Mode",
|
|
10
|
+
"ErrorCorrectionLevel",
|
|
11
|
+
"Symbol",
|
|
12
|
+
"FieldElement",
|
|
13
|
+
"Generator",
|
|
14
|
+
"Sequence",
|
|
15
|
+
"Literal",
|
|
16
|
+
"Iterable",
|
|
17
|
+
"Callable",
|
|
18
|
+
"to_bits",
|
|
19
|
+
"to_byte",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class FieldElement[T](Protocol):
|
|
24
|
+
def __add__(self: T, other: T, /) -> T: ...
|
|
25
|
+
def __sub__(self: T, other: T, /) -> T: ...
|
|
26
|
+
def __mul__(self: T, other: T, /) -> T: ...
|
|
27
|
+
def __truediv__(self: T, other: T, /) -> T: ...
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
type Bit = Literal[0, 1]
|
|
31
|
+
type Byte = int # 0-255
|
|
32
|
+
type Mode = Literal["numeric", "alphanumeric", "byte"]
|
|
33
|
+
type ErrorCorrectionLevel = Literal["L", "M", "Q", "H"]
|
|
34
|
+
type Symbol = np.ndarray[tuple[int, int], np.dtype[np.bool]]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def to_bits(value: int, length: int) -> Generator[Bit]:
|
|
38
|
+
"""Generate the lowest `length` bits of `value`, highest bit first."""
|
|
39
|
+
if length < 1:
|
|
40
|
+
raise ValueError("length must be positive")
|
|
41
|
+
bit_position = 1 << length - 1
|
|
42
|
+
while bit_position:
|
|
43
|
+
yield 1 if value & bit_position else 0
|
|
44
|
+
bit_position >>= 1
|
|
45
|
+
|
|
46
|
+
def to_byte(bits: tuple[Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit]) -> Byte:
|
|
47
|
+
"""Convert a tuple of 8 bits to its numeric value as a byte (uint8)."""
|
|
48
|
+
byte = 0
|
|
49
|
+
for bit in bits:
|
|
50
|
+
byte = (byte << 1) + bit
|
|
51
|
+
return byte
|