You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

390 lines
9.5 KiB

4 years ago
  1. =============================
  2. Pure python QR Code generator
  3. =============================
  4. Generate QR codes.
  5. For a standard install (which will include pillow_ for generating images),
  6. run::
  7. pip install qrcode[pil]
  8. .. _pillow: https://pypi.python.org/pypi/Pillow
  9. What is a QR Code?
  10. ==================
  11. A Quick Response code is a two-dimensional pictographic code used for its fast
  12. readability and comparatively large storage capacity. The code consists of
  13. black modules arranged in a square pattern on a white background. The
  14. information encoded can be made up of any kind of data (e.g., binary,
  15. alphanumeric, or Kanji symbols)
  16. Usage
  17. =====
  18. From the command line, use the installed ``qr`` script::
  19. qr "Some text" > test.png
  20. Or in Python, use the ``make`` shortcut function:
  21. .. code:: python
  22. import qrcode
  23. img = qrcode.make('Some data here')
  24. Advanced Usage
  25. --------------
  26. For more control, use the ``QRCode`` class. For example:
  27. .. code:: python
  28. import qrcode
  29. qr = qrcode.QRCode(
  30. version=1,
  31. error_correction=qrcode.constants.ERROR_CORRECT_L,
  32. box_size=10,
  33. border=4,
  34. )
  35. qr.add_data('Some data')
  36. qr.make(fit=True)
  37. img = qr.make_image(fill_color="black", back_color="white")
  38. The ``version`` parameter is an integer from 1 to 40 that controls the size of
  39. the QR Code (the smallest, version 1, is a 21x21 matrix).
  40. Set to ``None`` and use the ``fit`` parameter when making the code to determine
  41. this automatically.
  42. ``fill_color`` and ``back_color`` can change the background and the painting
  43. color of the QR, when using the default image factory.
  44. The ``error_correction`` parameter controls the error correction used for the
  45. QR Code. The following four constants are made available on the ``qrcode``
  46. package:
  47. ``ERROR_CORRECT_L``
  48. About 7% or less errors can be corrected.
  49. ``ERROR_CORRECT_M`` (default)
  50. About 15% or less errors can be corrected.
  51. ``ERROR_CORRECT_Q``
  52. About 25% or less errors can be corrected.
  53. ``ERROR_CORRECT_H``.
  54. About 30% or less errors can be corrected.
  55. The ``box_size`` parameter controls how many pixels each "box" of the QR code
  56. is.
  57. The ``border`` parameter controls how many boxes thick the border should be
  58. (the default is 4, which is the minimum according to the specs).
  59. Other image factories
  60. =====================
  61. You can encode as SVG, or use a new pure Python image processor to encode to
  62. PNG images.
  63. The Python examples below use the ``make`` shortcut. The same ``image_factory``
  64. keyword argument is a valid option for the ``QRCode`` class for more advanced
  65. usage.
  66. SVG
  67. ---
  68. You can create the entire SVG or an SVG fragment. When building an entire SVG
  69. image, you can use the factory that combines as a path (recommended, and
  70. default for the script) or a factory that creates a simple set of rectangles.
  71. From your command line::
  72. qr --factory=svg-path "Some text" > test.svg
  73. qr --factory=svg "Some text" > test.svg
  74. qr --factory=svg-fragment "Some text" > test.svg
  75. Or in Python:
  76. .. code:: python
  77. import qrcode
  78. import qrcode.image.svg
  79. if method == 'basic':
  80. # Simple factory, just a set of rects.
  81. factory = qrcode.image.svg.SvgImage
  82. elif method == 'fragment':
  83. # Fragment factory (also just a set of rects)
  84. factory = qrcode.image.svg.SvgFragmentImage
  85. else:
  86. # Combined path factory, fixes white space that may occur when zooming
  87. factory = qrcode.image.svg.SvgPathImage
  88. img = qrcode.make('Some data here', image_factory=factory)
  89. Two other related factories are available that work the same, but also fill the
  90. background of the SVG with white::
  91. qrcode.image.svg.SvgFillImage
  92. qrcode.image.svg.SvgPathFillImage
  93. Pure Python PNG
  94. ---------------
  95. Install the following two packages::
  96. pip install git+git://github.com/ojii/pymaging.git#egg=pymaging
  97. pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png
  98. From your command line::
  99. qr --factory=pymaging "Some text" > test.png
  100. Or in Python:
  101. .. code:: python
  102. import qrcode
  103. from qrcode.image.pure import PymagingImage
  104. img = qrcode.make('Some data here', image_factory=PymagingImage)
  105. ==========
  106. Change log
  107. ==========
  108. 6.0 (23 March 2018)
  109. ===================
  110. - Fix optimize length being ignored in ``QRCode.add_data``.
  111. - Better calculation of the best mask pattern and related optimizations. Big
  112. thanks to cryptogun!
  113. 5.3 (18 May 2016)
  114. =================
  115. * Fix incomplete block table for QR version 15. Thanks Rodrigo Queiro for the
  116. report and Jacob Welsh for the investigation and fix.
  117. * Avoid unnecessary dependency for non MS platforms, thanks to Noah Vesely.
  118. * Make ``BaseImage.get_image()`` actually work.
  119. 5.2 (25 Jan 2016)
  120. =================
  121. * Add ``--error-correction`` option to qr script.
  122. * Fix script piping to stdout in Python 3 and reading non-UTF-8 characters in
  123. Python 3.
  124. * Fix script piping in Windows.
  125. * Add some useful behind-the-curtain methods for tinkerers.
  126. * Fix terminal output when using Python 2.6
  127. * Fix terminal output to display correctly on MS command line.
  128. 5.2.1
  129. -----
  130. * Small fix to terminal output in Python 3 (and fix tests)
  131. 5.2.2
  132. -----
  133. * Revert some terminal changes from 5.2 that broke Python 3's real life tty
  134. code generation and introduce a better way from Jacob Welsh.
  135. 5.1 (22 Oct 2014)
  136. =================
  137. * Make ``qr`` script work in Windows. Thanks Ionel Cristian Mărieș
  138. * Fixed print_ascii function in Python 3.
  139. * Out-of-bounds code version numbers are handled more consistently with a
  140. ValueError.
  141. * Much better test coverage (now only officially supporting Python 2.6+)
  142. 5.0 (17 Jun 2014)
  143. =================
  144. * Speed optimizations.
  145. * Change the output when using the ``qr`` script to use ASCII rather than
  146. just colors, better using the terminal real estate.
  147. * Fix a bug in passing bytecode data directly when in Python 3.
  148. * Substation speed optimizations to best-fit algorithm (thanks Jacob Welsh!).
  149. * Introduce a ``print_ascii`` method and use it as the default for the ``qr``
  150. script rather than ``print_tty``.
  151. 5.0.1
  152. -----
  153. * Update version numbers correctly.
  154. 4.0 (4 Sep 2013)
  155. ================
  156. * Made qrcode work on Python 2.4 - Thanks tcely.
  157. Note: officially, qrcode only supports 2.5+.
  158. * Support pure-python PNG generation (via pymaging) for Python 2.6+ -- thanks
  159. Adam Wisniewski!
  160. * SVG image generation now supports alternate sizing (the default box size of
  161. 10 == 1mm per rectangle).
  162. * SVG path image generation allows cleaner SVG output by combining all QR rects
  163. into a single path. Thank you, Viktor Stískala.
  164. * Added some extra simple SVG factories that fill the background white.
  165. 4.0.1
  166. -----
  167. * Fix the pymaging backend not able to save the image to a buffer. Thanks ilj!
  168. 4.0.2
  169. -----
  170. * Fix incorrect regex causing a comma to be considered part of the alphanumeric
  171. set.
  172. * Switch to using setuptools for setup.py.
  173. 4.0.3
  174. -----
  175. * Fix bad QR code generation due to the regex comma fix in version 4.0.2.
  176. 4.0.4
  177. -----
  178. * Bad version number for previous hotfix release.
  179. 3.1 (12 Aug 2013)
  180. =================
  181. * Important fixes for incorrect matches of the alpha-numeric encoding mode.
  182. Previously, the pattern would match if a single line was alpha-numeric only
  183. (even if others wern't). Also, the two characters ``{`` and ``}`` had snuck
  184. in as valid characters. Thanks to Eran Tromer for the report and fix.
  185. * Optimized chunking -- if the parts of the data stream can be encoded more
  186. efficiently, the data will be split into chunks of the most efficient modes.
  187. 3.1.1
  188. -----
  189. * Update change log to contain version 3.1 changes. :P
  190. * Give the ``qr`` script an ``--optimize`` argument to control the chunk
  191. optimization setting.
  192. 3.0 (25 Jun 2013)
  193. =================
  194. * Python 3 support.
  195. * Add QRCode.get_matrix, an easy way to get the matrix array of a QR code
  196. including the border. Thanks Hugh Rawlinson.
  197. * Add in a workaround so that Python 2.6 users can use SVG generation (they
  198. must install ``lxml``).
  199. * Some initial tests! And tox support (``pip install tox``) for testing across
  200. Python platforms.
  201. 2.7 (5 Mar 2013)
  202. ================
  203. * Fix incorrect termination padding.
  204. 2.6 (2 Apr 2013)
  205. ================
  206. * Fix the first four columns incorrectly shifted by one. Thanks to Josep
  207. Gómez-Suay for the report and fix.
  208. * Fix strings within 4 bits of the QR version limit being incorrectly
  209. terminated. Thanks to zhjie231 for the report.
  210. 2.5 (12 Mar 2013)
  211. =================
  212. * The PilImage wrapper is more transparent - you can use any methods or
  213. attributes available to the underlying PIL Image instance.
  214. * Fixed the first column of the QR Code coming up empty! Thanks to BecoKo.
  215. 2.5.1
  216. -----
  217. * Fix installation error on Windows.
  218. 2.4 (23 Apr 2012)
  219. =================
  220. * Use a pluggable backend system for generating images, thanks to Branko Čibej!
  221. Comes with PIL and SVG backends built in.
  222. 2.4.1
  223. -----
  224. * Fix a packaging issue
  225. 2.4.2
  226. -----
  227. * Added a ``show`` method to the PIL image wrapper so the ``run_example``
  228. function actually works.
  229. 2.3 (29 Jan 2012)
  230. =================
  231. * When adding data, auto-select the more efficient encoding methods for numbers
  232. and alphanumeric data (KANJI still not supported).
  233. 2.3.1
  234. -----
  235. * Encode unicode to utf-8 bytestrings when adding data to a QRCode.
  236. 2.2 (18 Jan 2012)
  237. =================
  238. * Fixed tty output to work on both white and black backgrounds.
  239. * Added `border` parameter to allow customizing of the number of boxes used to
  240. create the border of the QR code
  241. 2.1 (17 Jan 2012)
  242. =================
  243. * Added a ``qr`` script which can be used to output a qr code to the tty using
  244. background colors, or to a file via a pipe.