diff --git a/README.rst b/README.rst index ec383462..ce7fa9f4 100644 --- a/README.rst +++ b/README.rst @@ -43,196 +43,13 @@ Or in Python, use the ``make`` shortcut function: type(img) # qrcode.image.pil.PilImage img.save("some_file.png") -Advanced Usage --------------- +For more info check `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_H) - 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") +Features +======== +* Generate QR Code as either PNG, SVG or SVG Fragment. +* Convert the SVG image into strings using the ``to_string()`` method. +* Style image, by customizing colors, shapes and embedding images. Examples ======== @@ -271,3 +88,5 @@ Alternative to piping output to file to avoid PowerShell issues:: # qr "Some data" > test.png qr --output=test.png "Some data" + +More examples in `Advanced Usage `_. diff --git a/doc/color_masks.png b/doc/color_masks.png deleted file mode 100644 index e97826a6..00000000 Binary files a/doc/color_masks.png and /dev/null differ diff --git a/doc/module_drawers.png b/doc/module_drawers.png deleted file mode 100644 index 43984832..00000000 Binary files a/doc/module_drawers.png and /dev/null differ diff --git a/doc/qr.1 b/doc/qr.1 deleted file mode 100644 index 71be0892..00000000 --- a/doc/qr.1 +++ /dev/null @@ -1,50 +0,0 @@ -.\" Manpage for qr -.TH QR 1 "6 Feb 2023" "7.4.2" "Python QR tool" -.SH NAME -qr \- script to create QR codes at the command line -.SH SYNOPSIS -qr [\-\-help] [\-\-factory=FACTORY] [\-\-optimize=OPTIMIZE] [\-\-error\-correction=LEVEL] [data] -.SH DESCRIPTION -This script uses the python qrcode module. It can take data from stdin or from the commandline and generate a QR code. -Normally it will output the QR code as ascii art to the terminal. If the output is piped to a file, it will output the image (default type of PNG). -.SH OPTIONS -.PP -\fB\ \-h, \-\-help\fR -.RS 4 -Show a help message. -.RE - -.PP -\fB\ \-\-factory=FACTORY\fR -.RS 4 -Full python path to the image factory class to create the -image with. You can use the following shortcuts to the -built-in image factory classes: pil (default), png ( -default if pillow is not installed), svg, svg-fragment, -svg-path. -.RE - -.PP -\fB\ \-\-optimize=OPTIMIZE\fR -.RS 4 -Optimize the data by looking for chunks of at least this -many characters that could use a more efficient encoding -method. Use 0 to turn off chunk optimization. -.RE - -.PP -\fB\ \-\-error\-correction=LEVEL\fR -.RS 4 -The error correction level to use. Choices are L (7%), -M (15%, default), Q (25%), and H (30%). -.RE - -.PP -\fB\ data\fR -.RS 4 -The data from which the QR code will be generated. -.RE - -.SH SEE ALSO -https://github.com/lincolnloop/python-qrcode/ - diff --git a/docs/advanced_usage.rst b/docs/advanced_usage.rst new file mode 100644 index 00000000..d941627c --- /dev/null +++ b/docs/advanced_usage.rst @@ -0,0 +1,195 @@ +Advanced Usage +============== + +Using the python `QRCode <../qrcode/main.py#L77>`__ class. + +.. code:: python + + class QRCode( + version: Any | None = None, + error_correction: int = ERROR_CORRECT_M, + box_size: int = 10, + border: int = 4, + image_factory: BaseImageWithDrawer | None = None, + mask_pattern: int | None = None + ) + +Parameters of ``QRCode`` +------------------------ + +``version`` +~~~~~~~~~~~ + +Any integer from 1 to 40. Controls the size of the QR Code. Set to ``None`` +and use the ``fit`` parameter when making the code to determine this +automatically. + ++---------+---------+--------------------+ +| version | size | max content length | ++=========+=========+====================+ +| `1` | 21x21 | 4 ASCII chars | ++---------+---------+--------------------+ +| `2` | 25x25 | 9 ASCII chars | ++---------+---------+--------------------+ +| `3` | 29x29 | 17 ASCII chars | ++---------+---------+--------------------+ +| `4` | 33x33 | 50 ASCII chars | ++---------+---------+--------------------+ +| `10` | 57x57 | 174 ASCII chars | ++---------+---------+--------------------+ +| `25` | 117x117 | 1269 ASCII chars | ++---------+---------+--------------------+ +| `40` | 177x177 | 1852 ASCII chars | ++---------+---------+--------------------+ + +`Check out the example code here <./examples/version.py>`__ + +.. image:: ./examples/version.png + +``error_correction`` +~~~~~~~~~~~~~~~~~~~~ + +Controls how many errors can be corrected during scanning of the QR Code. + ++---------------------+--------------------------------+ +| value | percentage of errors corrected | ++=====================+================================+ +| ``ERROR_CORRECT_L`` | 7% | ++---------------------+--------------------------------+ +| ``ERROR_CORRECT_M`` | 15% | ++---------------------+--------------------------------+ +| ``ERROR_CORRECT_Q`` | 25% | ++---------------------+--------------------------------+ +| ``ERROR_CORRECT_H`` | 30% | ++---------------------+--------------------------------+ + + +`Check out the example code here <./examples/error_correction.py>`__ + +.. image:: ./examples/error_correction.png + +``box_size`` +~~~~~~~~~~~~ + +Controls how many pixels each _box of the QR code is. + +``border`` +~~~~~~~~~~ + +Controls how many boxes thick the border should be. The default is 4, +which is the minimum according to the specs. + +``image_factory`` +~~~~~~~~~~~~~~~~~ + +You can encode it as SVG, or use a new pure Python image processor to +encode to PNG images. + +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 apply a colour mask to the QRCode, use the StyledPilImage. It accepts an +optional ``color_mask`` and ``embeded_image_path``. + ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| value | usage | edit style | edit color mask | ++======================+=======================================================================+============+=================+ +| ``SvgImage`` | Simple factory, just a set of rects | ✓ | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``SvgFragmentImage`` | Fragment factory (also just a set of rects) | ✓ | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``SvgPathImage`` | Combined path factory, fixes white space that may occur when zooming | ✓ | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``SvgFillImage`` | Similar to ``SvgImage``, but also fills the background with white | ✓ | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``SvgPathFillImage`` | Similar to ``SvgPathImage``, but also fills the background with white | ✓ | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``PyPNGImage`` | PNG encoder that uses pypng | | | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ +| ``StyledPilImage`` | PNG encoder that uses Pillow | ✓ | ✓ | ++----------------------+-----------------------------------------------------------------------+------------+-----------------+ + +`Check out the example code here <./examples/image_factory.py>`__ + +.. image:: ./examples/image_factory.png + +``mask_pattern`` +~~~~~~~~~~~~~~~~ + +Any integer from 0 to 7. Controls which mask to use. QR Codes don’t work +good with large areas of black and white, so XOR masks are applied to +make them easier to scan. + +`Check out the example code here <./examples/mask_pattern.py>`__ + +.. image:: ./examples/mask_pattern.png + +Parameters of ``make_image()`` +------------------------------ + +``back_color`` +~~~~~~~~~~~~~~ + +Controls the colour of the background. It can be a name, hex string or rgb +tuple. + +`Check out the example code here <./examples/back_color.py>`__ + +.. image:: ./examples/back_color.png + +``fill_color`` +~~~~~~~~~~~~~~ + +Controls the colour of the QR Code pattern. It can be a name, hex string or +RGB tuple. + +`Check out the example code here <./examples/fill_color.py>`__ + +.. image:: ./examples/fill_color.png + +``color_mask`` +~~~~~~~~~~~~~~ + +Controls the shading of the QR Code pattern. Only works with +``StyledPilImage`` image_factory. The colours have to be defined as +tuples of ints. The image has to be PNG. + +`Check out the example code here <./examples/color_mask.py>`__ + +.. image:: ./examples/color_mask.png + +``module_drawer`` +~~~~~~~~~~~~~~~~ + +Controls the style of drawn “boxes” of the QR Code. + ++------------------------------+------------------------------+---------------------------------------------+ +| style | png | svg | ++==============================+==============================+=============================================+ +| squares | ``SquareModuleDrawer`` | ``SvgSquareDrawer`` ``SvgPathSquareDrawer`` | ++------------------------------+------------------------------+---------------------------------------------+ +| gapped squares | ``GappedSquareModuleDrawer`` | | ++------------------------------+------------------------------+---------------------------------------------+ +| circles | ``CircleModuleDrawer`` | ``SvgCircleDrawer`` ``SvgPathCircleDrawer`` | ++------------------------------+------------------------------+---------------------------------------------+ +| rounded and connected blobs | ``RoundedModuleDrawer`` | | ++------------------------------+------------------------------+---------------------------------------------+ +| vertical bars | ``VerticalBarsDrawer`` | | ++------------------------------+------------------------------+---------------------------------------------+ +| horizontal bars | ``HorizontalBarsDrawer`` | | ++------------------------------+------------------------------+---------------------------------------------+ + +`Check out the example code here <./examples/module_drawer.py>`__ + +.. image:: ./examples/module_drawer.png + +``embeded_image_path`` +~~~~~~~~~~~~~~~~~~~~~~ + +Allows the embedding of PNG images in the centre of the QR code. Only works with +``StyledPilImage`` image_factory. + +`Check out the example code here <./examples/embeded_image_path.py>`__ + +.. image:: ./examples/embeded_image_path.png diff --git a/docs/examples/_make_example.py b/docs/examples/_make_example.py new file mode 100644 index 00000000..00a670c1 --- /dev/null +++ b/docs/examples/_make_example.py @@ -0,0 +1,56 @@ +''' +Test multiple versions of specific parameters and create a comparison image. +Usage: + python _make_example.py [-h] -e EXAMPLE -o OUTPUT -p PARAM [PARAM ...] [-c COLUMNS] +''' + +import os +import io +import math +import argparse +import cairosvg +import matplotlib.pyplot as plt +from PIL import Image + +DATA = 'https://github.com/lincolnloop/python-qrcode' + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('-e', '--example', required=True, help='Path to the example script from ./docs/examples') + parser.add_argument('-p', '--param', required=True, nargs='+', help='List of parameters to test') + parser.add_argument('-c', '--columns', help='Number of columns in the image. Defaults to number of parameters.', type=int) + return parser.parse_args() + + +def generate_images(example_filename: str, params: list[str]): + images = [] + for param in params: + status = os.system(f'python {example_filename} "{DATA}" "{param}"') + if status != 0: + raise RuntimeError(f'Error creating image for {param}') + if os.path.exists('example.png'): + images.append(Image.open('example.png')) + if os.path.exists('example.svg'): + png_bytes = cairosvg.svg2png(url='example.svg') + images.append(Image.open(io.BytesIO(png_bytes))) + os.system('find example.png example.svg -delete 2>/dev/null') + return images + + +def create_comparison_image(images: list[Image.Image], params: list[str], output_filename: str, columns: int): + rows = math.ceil(len(params) / columns) + fig, axes = plt.subplots(rows, columns, figsize=(columns * 3, rows * 3 + 0.5)) + for ax, img, label in zip(axes.flat, images, params): + ax.imshow(img) + ax.set_title(label) + for ax in axes.flat: + ax.axis('off') + fig.tight_layout() + plt.savefig(output_filename) + + +if __name__ == '__main__': + args = parse_args() + images = generate_images(args.example, args.param) + create_comparison_image(images, args.param, args.example.replace('.py', '.png'), args.columns or len(args.param)) diff --git a/docs/examples/back_color.png b/docs/examples/back_color.png new file mode 100644 index 00000000..725eede6 Binary files /dev/null and b/docs/examples/back_color.png differ diff --git a/docs/examples/back_color.py b/docs/examples/back_color.py new file mode 100644 index 00000000..840a49ed --- /dev/null +++ b/docs/examples/back_color.py @@ -0,0 +1,23 @@ +''' +Create a QR Code with a specific QRCode make() function parameter: back_color. +Usage: + python back_color.py [data] [back_color] +''' + +import sys +import qrcode + +assert len(sys.argv) == 3, 'Usage: python back_color.py [data] [back_color]' + +DATA = sys.argv[1] + +# string or tuple[int, int, int] +BACK_COL = sys.argv[2] +if '(' in BACK_COL and ')' in BACK_COL: + BACK_COL = eval(BACK_COL) + +qr = qrcode.QRCode() +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image(back_color=BACK_COL) +img.save("example.png") diff --git a/docs/examples/color_mask.png b/docs/examples/color_mask.png new file mode 100644 index 00000000..62c2c013 Binary files /dev/null and b/docs/examples/color_mask.png differ diff --git a/docs/examples/color_mask.py b/docs/examples/color_mask.py new file mode 100644 index 00000000..73893a4b --- /dev/null +++ b/docs/examples/color_mask.py @@ -0,0 +1,36 @@ +''' +Create a QR Code with a specific QRCode make() function parameter: color_mask. +Usage: + python color_mask.py [data] [color_mask] +''' + +import sys +import qrcode +import qrcode.image.styledpil +import qrcode.image.styles.colormasks + +RED = (255, 0, 0) +GREEN = (0, 255, 0) +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) + +assert len(sys.argv) == 3, 'Usage: python color_mask.py [data] [color_mask]' + +DATA = sys.argv[1] +COL_MASK = { + 'SolidFillColorMask': qrcode.image.styles.colormasks.SolidFillColorMask(back_color=WHITE, front_color=BLACK), + 'RadialGradiantColorMask': qrcode.image.styles.colormasks.RadialGradiantColorMask(back_color=WHITE, center_color=RED, edge_color=GREEN), + 'SquareGradiantColorMask': qrcode.image.styles.colormasks.SquareGradiantColorMask(back_color=WHITE, center_color=RED, edge_color=GREEN), + 'HorizontalGradiantColorMask': qrcode.image.styles.colormasks.HorizontalGradiantColorMask(back_color=WHITE, left_color=RED, right_color=GREEN), + 'VerticalGradiantColorMask': qrcode.image.styles.colormasks.VerticalGradiantColorMask(back_color=WHITE, top_color=RED, bottom_color=GREEN), + 'ImageColorMask': qrcode.image.styles.colormasks.ImageColorMask(color_mask_path='stars.png'), +}[sys.argv[2]] + +qr = qrcode.QRCode( + error_correction=qrcode.ERROR_CORRECT_H, + image_factory=qrcode.image.styledpil.StyledPilImage +) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image(color_mask=COL_MASK) +img.save(f"example.png") diff --git a/docs/examples/embeded_image_path.png b/docs/examples/embeded_image_path.png new file mode 100644 index 00000000..798bd492 Binary files /dev/null and b/docs/examples/embeded_image_path.png differ diff --git a/docs/examples/embeded_image_path.py b/docs/examples/embeded_image_path.py new file mode 100644 index 00000000..35f16343 --- /dev/null +++ b/docs/examples/embeded_image_path.py @@ -0,0 +1,25 @@ +''' +Create a QR Code with a specific QRCode make() function parameter: embeded_image_path. +Usage: + python embeded_image_path.py [data] [embeded_image_path] +''' + +import sys +import qrcode +import qrcode.image.styledpil + +assert len(sys.argv) == 3, 'Usage: python embeded_image_path.py [data] [embeded_image_path]' + +DATA = sys.argv[1] +EM_IMG_PATH = sys.argv[2] +if EM_IMG_PATH == 'None': + EM_IMG_PATH = None + +qr = qrcode.QRCode( + error_correction=qrcode.ERROR_CORRECT_H, + image_factory=qrcode.image.styledpil.StyledPilImage +) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image(embeded_image_path=EM_IMG_PATH) +img.save(f"example.png") diff --git a/docs/examples/error_correction.png b/docs/examples/error_correction.png new file mode 100644 index 00000000..889aec59 Binary files /dev/null and b/docs/examples/error_correction.png differ diff --git a/docs/examples/error_correction.py b/docs/examples/error_correction.py new file mode 100644 index 00000000..3d2b4fc1 --- /dev/null +++ b/docs/examples/error_correction.py @@ -0,0 +1,24 @@ +''' +Create a QR Code with a specific QRCode class parameter: error_correction. +Usage: + python error_correction.py [data] [error_correction] +''' + +import sys +import qrcode + +assert len(sys.argv) == 3, 'Usage: python error_correction.py [data] [error_correction]' + +DATA = sys.argv[1] +ERR_COR = { + 'ERROR_CORRECT_L': qrcode.ERROR_CORRECT_L, + 'ERROR_CORRECT_M': qrcode.ERROR_CORRECT_M, + 'ERROR_CORRECT_Q': qrcode.ERROR_CORRECT_Q, + 'ERROR_CORRECT_H': qrcode.ERROR_CORRECT_H, +}[sys.argv[2]] + +qr = qrcode.QRCode(error_correction=ERR_COR) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image() +img.save("example.png") diff --git a/docs/examples/fill_color.png b/docs/examples/fill_color.png new file mode 100644 index 00000000..332fba4c Binary files /dev/null and b/docs/examples/fill_color.png differ diff --git a/docs/examples/fill_color.py b/docs/examples/fill_color.py new file mode 100644 index 00000000..304f257d --- /dev/null +++ b/docs/examples/fill_color.py @@ -0,0 +1,23 @@ +''' +Create a QR Code with a specific QRCode make() function parameter: fill_color. +Usage: + python fill_color.py [data] [fill_color] +''' + +import sys +import qrcode + +assert len(sys.argv) == 3, 'Usage: python fill_color.py [data] [fill_color]' + +DATA = sys.argv[1] + +# string or tuple[int, int, int] +FILL_COL = sys.argv[2] +if '(' in FILL_COL and ')' in FILL_COL: + FILL_COL = eval(FILL_COL) + +qr = qrcode.QRCode() +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image(fill_color=FILL_COL) +img.save("example.png") diff --git a/docs/examples/github-icon.png b/docs/examples/github-icon.png new file mode 100644 index 00000000..1a97f78b Binary files /dev/null and b/docs/examples/github-icon.png differ diff --git a/docs/examples/image_factory.png b/docs/examples/image_factory.png new file mode 100644 index 00000000..1e8bafd4 Binary files /dev/null and b/docs/examples/image_factory.png differ diff --git a/docs/examples/image_factory.py b/docs/examples/image_factory.py new file mode 100644 index 00000000..8b3bbd39 --- /dev/null +++ b/docs/examples/image_factory.py @@ -0,0 +1,31 @@ +''' +Create a QR Code with a specific QRCode class parameter: image_factory. +Usage: + python image_factory.py [data] [image_factory] +''' + +import sys +import qrcode +import qrcode.image.svg +import qrcode.image.pure +import qrcode.image.styledpil + +assert len(sys.argv) == 3, 'Usage: python image_factory.py [data] [image_factory]' + +DATA = sys.argv[1] +IMG_FAC = { + 'SvgImage': qrcode.image.svg.SvgImage, + 'SvgFragmentImage': qrcode.image.svg.SvgFragmentImage, + 'SvgPathImage': qrcode.image.svg.SvgPathImage, + 'SvgFillImage': qrcode.image.svg.SvgFillImage, + 'SvgPathFillImage': qrcode.image.svg.SvgPathFillImage, + 'PyPNGImage': qrcode.image.pure.PyPNGImage, + 'StyledPilImag': qrcode.image.styledpil.StyledPilImage +}[sys.argv[2]] + +qr = qrcode.QRCode(image_factory=IMG_FAC) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image() +ext = "svg" if "Svg" in sys.argv[2] else "png" +img.save(f"example.{ext}") diff --git a/docs/examples/mask_pattern.png b/docs/examples/mask_pattern.png new file mode 100644 index 00000000..cf47c950 Binary files /dev/null and b/docs/examples/mask_pattern.png differ diff --git a/docs/examples/mask_pattern.py b/docs/examples/mask_pattern.py new file mode 100644 index 00000000..e742d96d --- /dev/null +++ b/docs/examples/mask_pattern.py @@ -0,0 +1,19 @@ +''' +Create a QR Code with a specific QRCode class parameter: mask_pattern. +Usage: + python mask_pattern.py [data] [mask_pattern] +''' + +import sys +import qrcode + +assert len(sys.argv) == 3, 'Usage: python mask_pattern.py [data] [mask_pattern]' + +DATA = sys.argv[1] +MASK_PAT = int(sys.argv[2]) + +qr = qrcode.QRCode(mask_pattern=MASK_PAT) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image() +img.save(f"example.png") diff --git a/docs/examples/module_drawer.png b/docs/examples/module_drawer.png new file mode 100644 index 00000000..d894f364 Binary files /dev/null and b/docs/examples/module_drawer.png differ diff --git a/docs/examples/module_drawer.py b/docs/examples/module_drawer.py new file mode 100644 index 00000000..f877bb10 --- /dev/null +++ b/docs/examples/module_drawer.py @@ -0,0 +1,34 @@ +''' +Create a QR Code with a specific QRCode make() function parameter: module_drawer. +Usage: + python module_drawer.py [data] [module_drawer] +''' + +import sys +import qrcode +import qrcode.image.svg +import qrcode.image.styledpil +import qrcode.image.styles.moduledrawers.svg + +assert len(sys.argv) == 3, 'Usage: python module_drawer.py [data] [module_drawer]' + +DATA = sys.argv[1] +MOD_DRAW, FACTORY = { + 'SvgSquareDrawer': (qrcode.image.styles.moduledrawers.svg.SvgSquareDrawer(), qrcode.image.svg.SvgFillImage), + 'SvgCircleDrawer': (qrcode.image.styles.moduledrawers.svg.SvgCircleDrawer(), qrcode.image.svg.SvgFillImage), + 'SvgPathSquareDrawer': (qrcode.image.styles.moduledrawers.svg.SvgPathSquareDrawer(), qrcode.image.svg.SvgPathFillImage), + 'SvgPathCircleDrawer': (qrcode.image.styles.moduledrawers.svg.SvgPathCircleDrawer(), qrcode.image.svg.SvgPathFillImage), + 'SquareModuleDrawer': (qrcode.image.styles.moduledrawers.SquareModuleDrawer(), qrcode.image.styledpil.StyledPilImage), + 'GappedSquareModuleDrawer': (qrcode.image.styles.moduledrawers.GappedSquareModuleDrawer(), qrcode.image.styledpil.StyledPilImage), + 'CircleModuleDrawer': (qrcode.image.styles.moduledrawers.CircleModuleDrawer(), qrcode.image.styledpil.StyledPilImage), + 'RoundedModuleDrawer': (qrcode.image.styles.moduledrawers.RoundedModuleDrawer(), qrcode.image.styledpil.StyledPilImage), + 'VerticalBarsDrawer': (qrcode.image.styles.moduledrawers.VerticalBarsDrawer(), qrcode.image.styledpil.StyledPilImage), + 'HorizontalBarsDrawer': (qrcode.image.styles.moduledrawers.HorizontalBarsDrawer(), qrcode.image.styledpil.StyledPilImage), +}[sys.argv[2]] + +qr = qrcode.QRCode(error_correction=qrcode.ERROR_CORRECT_H, image_factory=FACTORY) +qr.add_data(DATA) +qr.make(fit=True) +img = qr.make_image(module_drawer=MOD_DRAW) +ext = "svg" if "Svg" in sys.argv[2] else "png" +img.save(f"example.{ext}") diff --git a/docs/examples/stars.png b/docs/examples/stars.png new file mode 100644 index 00000000..aa1efe94 Binary files /dev/null and b/docs/examples/stars.png differ diff --git a/docs/examples/version.png b/docs/examples/version.png new file mode 100644 index 00000000..450aa0b5 Binary files /dev/null and b/docs/examples/version.png differ diff --git a/docs/examples/version.py b/docs/examples/version.py new file mode 100644 index 00000000..4ac50378 --- /dev/null +++ b/docs/examples/version.py @@ -0,0 +1,22 @@ +''' +Create a QR Code with a specific QRCode class parameter: version. +Usage: + python version.py [data] [version] +''' + +import sys +import qrcode + +assert len(sys.argv) == 3, 'Usage: python version.py [data] [version]' + +DATA = sys.argv[1] +VERSION = sys.argv[2] +try: VERSION = int(VERSION) +except ValueError: VERSION = None + +qr = qrcode.QRCode(version=VERSION) +qr.add_data(DATA) +if VERSION is None: + qr.make(fit=True) +img = qr.make_image() +img.save("example.png")