File: serializers.rst

package info (click to toggle)
segno 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,072 kB
  • sloc: python: 9,936; makefile: 162; javascript: 16
file content (300 lines) | stat: -rw-r--r-- 9,890 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
QR code and Micro QR code serialization
=======================================

A QR code or Micro QR code is independent of its output, it's just a matrix.
To save a QR code or Micro QR code, Segno provides several output formats.

Segno provides a :py:func:`segno.QRCode.save` method to serialize a (Micro)
QR code in different formats:

.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make('Tomorrow Never Knows')
    >>> qrcode.save('tomorrow-never-knows.svg')
    >>> qrcode.save('tomorrow-never-knows.png')
    >>> qrcode.save('tomorrow-never-knows.eps')


.. image:: _static/tomorrow-never-knows-2-q.png
    :alt: 2-Q QR code encoding "Tomorrow Never Knows"


Border
------

All serializers accept a ``border`` parameter which indicates the "quiet zone"
of a (Micro) QR code. If ``border`` is ``None``, the default border (quiet zone)
size will be used. If the resulting (Micro) QR code should have no border or
a custom border, the user may specify the border explicitly.

.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make('Vampire Blues')
    >>> qrcode.save('vampire-blues.svg', border=0)  # No border

.. image:: _static/vampire-blues-m4-m-no-border.svg
    :alt: M4-M QR code encoding "Vampire Blues", quiet zone omitted


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make('Vampire Blues')
    >>> qrcode.save('vampire-blues.png', border=10)  # Larger border

.. image:: _static/vampire-blues-m4-m-border-10.png
    :alt: M4-M QR code encoding "Vampire Blues", quiet zone of 10


Scale
-----

Most serializers accept a ``scale`` parameter which indicates the scaling
factor of the serialization. By default, the scaling factor is ``1`` which means
that size of a dark / light module is interpreted as one unit in the specific
user space (i.e. 1 pixel for the :ref:`PNG <png>` serializer or
1 point (1/72 of an inch) in :ref:`EPS <eps>`). Some serializers
(like :ref:`PNG <png>`) accept only an integer value or convert the provided
scaling factor to an integer. Other, like :ref:`SVG <svg>` and :ref:`EPS <eps>`,
accept float values and do not "downgrade" it to an integer.

.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make_qr('The Beatles')
    >>> qrcode.save('the-beatles.png', scale=1.2)  # No scaling at all since int(1.2) is 1

.. image:: _static/the-beatles-1-q-scale-1.png
    :alt: 1-Q QR code encoding "The Beatles"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make_qr('The Beatles')
    >>> qrcode.save('the-beatles.png', scale=10)  # 1 module == 10 pixels

.. image:: _static/the-beatles-1-q-scale-10.png
    :alt: 1-Q QR code encoding "The Beatles"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make_qr('The Beatles')
    >>> qrcode.save('the-beatles.svg', scale=2.4)  # SVG accepts float values

.. image:: _static/the-beatles-1-q-scale-2.4.svg
    :alt: 1-Q QR code encoding "The Beatles"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make_qr('The Beatles')
    >>> # The SVG serializer provides the "unit" parameter to specify
    >>> # how to interpret the values
    >>> qrcode.save('the-beatles.svg', scale=10, unit='mm')  # 1 unit = 1 mm

.. image:: _static/the-beatles-1-q-scale-10-unit-mm.svg
    :alt: 1-Q QR code encoding "The Beatles"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make_qr('The Beatles')
    >>> qrcode.save('the-beatles.svg', unit='cm')  # 1 unit = 1 cm, result as above

.. image:: _static/the-beatles-1-q-scale-1-unit-cm.svg
    :alt: 1-Q QR code encoding "The Beatles"


Color of dark and light modules
-------------------------------

Many serializers accept the parameters ``dark`` and ``light`` to specify
the color of the dark modules and light modules. See :doc:`colorful-qrcodes`
for details.

.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> qrcode.save('sgt-peppers.svg', dark='darkred', light='lightblue')

.. image:: _static/sgt-peppers-dark_darkred-light_lightblue.svg
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> qrcode.save('sgt-peppers.svg', dark='#ccc')

.. image:: _static/sgt-peppers-dark_685e5c.svg
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> qrcode.save('sgt-peppers.png', light=None)  # Transparent background

.. image:: _static/sgt-peppers-light_transparent.png
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> # Dark modules = transparent, light modules = black
    >>> qrcode.save('sgt-peppers.png', dark=None, light='black')

.. image:: _static/sgt-peppers-dark_transparent-light_black.png
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> # Dark modules with alpha transparency
    >>> qrcode.save('sgt-peppers.png', dark='#0000ffcc')

.. image:: _static/sgt-peppers-dark_0000ffcc.png
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> qrcode.save('sgt-peppers.svg', dark='#00fc')  # Same as above but SVG

.. image:: _static/sgt-peppers-dark_00fc.svg
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
    >>> # Anonther color, save as compressed SVG
    >>> qrcode.save('sgt-peppers.svgz', dark=(8, 90, 117))

.. image:: _static/sgt-peppers-dark_890117.svg
    :alt: 3-M QR code encoding "Sgt. Pepper’s Lonely Hearts Club Band"


Saving QR codes to streams
--------------------------

If the QR code should be serialized to a buffer, use the
:paramref:`kind <segno.QRCode.save.kind>`  parameter to specify the output format.
Please note that some serializers write bytes while others write strings, see
:py:meth:`segno.QRCode.save` for details.

.. code-block:: python

    >>> import segno
    >>> import io
    >>> qrcode = segno.make('Paul McCartney')
    >>> buff = io.BytesIO()
    >>> qrcode.save(buff, kind='svg')
    >>> # All other serializer parameters are supported as well
    >>> buff = io.BytesIO()
    >>> qrcode.save(buff, kind='svg', dark='darkblue', light='#eee')

.. image:: _static/paul-mccartney.svg
    :alt: M4-L QR code encoding "Paul McCartney"


See :py:meth:`segno.QRCode.save` for a complete reference which parameters are
accepted by the specific serializer.


More colorful QR Codes
----------------------

The :ref:`SVG <svg>`, :ref:`PNG <png>` and :ref:`PPM <ppm>` serializers support
more than two colors.

.. code-block:: python

    >>> import segno
    >>> qrcode = segno.make('Yellow Submarine', version=7, error='h')
    >>> qrcode.save('qrcode_yellow-submarine.png', scale=5, dark='darkred',
    ...             data_dark='darkorange', data_light='yellow')

.. image:: _static/colorful/qrcode_yellow-submarine.png
    :alt: Colorful 7-H QR code encoding "Yellow Submarine"

See :doc:`colorful-qrcodes` for available options.


.. _serializers:

Available serializers
---------------------

ANSI
    ANSI escape code. The serializer supports the `border` keyword, only.
    See :ref:`ANSI <ansi>` for details.

EPS
    Encapsulated PostScript (EPS). The serializer provides all default features
    (scale, border, color of dark / light modules), see :ref:`EPS <eps>` for details.

LaTeX
    LaTeX / PGF/TikZ. The serializer provides no support to change the color
    of the light modules, but all other default features
    (scale, border, color) are supported, see :ref:`LaTeX <latex>` for details.

PAM
    Portable Arbitrary Map (PAM). The serializer provides all default features
    (scale, border, color of dark / light modules), see :ref:`PAM <pam>` for details.

PBM
    Portable Bitmap (PBM). The serializer does not support any coloring,
    but the common featurs like scale and border are supported, see :ref:`PBM <pbm>`
    for details.

PPM
    Portable Pixmap (PPM). The serializer does not support transparency,
    but the common features like scale, border and (multiple) colors are
    supported, see :ref:`PPM <ppm>` for details.

PDF
    Portable Document Format (PDF). The serializer provides all default features
    (scale, border, color of dark / light modules), see :ref:`PDF <pdf>` for details.

PNG
    Portable Network Graphics (PNG). The serializer provides all default features
    (scale, border, color of dark / light modules) and a few more to customize the
    output, see :ref:`PNG <png>` for details.

SVG
    Scalable Vector Graphics (SVG). The serializer provides all default features
    (scale, border, color of dark / light modules) and many more to customize the
    output, see :ref:`SVG <svg>` for details. SVGZ (compressed SVG) is supported
    as well.

TXT
    Text output. The serializer does not support any scale or color, but the
    characters for the dark and light modules may be specified,
    see :ref:`TXT <txt>` for details.

XBM
    X BitMap (XBM). The serializer does not support any coloring, but scale
    and border are supported, see :ref:`XBM <xbm>` for details.

XPM
    X PixMap (XPM). The serializer provides all default features
    (scale, border, color of dark / light modules) and a few more, see
    :ref:`XPM <xpm>` for details.