File: write_datamatrix.py

package info (click to toggle)
python-pylibdmtx 0.1.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: python: 1,093; makefile: 11; sh: 5
file content (50 lines) | stat: -rw-r--r-- 1,285 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
#!/usr/bin/env python
from __future__ import print_function

import argparse
import sys

import pylibdmtx
from pylibdmtx.pylibdmtx import (
    encode, ENCODING_SIZE_NAMES, ENCODING_SCHEME_NAMES
)


def main(args=None):
    if args is None:
        args = sys.argv[1:]

    parser = argparse.ArgumentParser(
        description='Writes a datamatrix barcode to a new image file'
    )
    parser.add_argument('file', help='Filename of the output image')
    parser.add_argument(
        'data', help='Data to be written; will be utf-8 encoded'
    )
    parser.add_argument(
        '--size',
        help="Encoding size (not image dimensions); default is 'ShapeAuto'",
        choices=ENCODING_SIZE_NAMES
    )
    parser.add_argument(
        '--scheme',
        help="Encoding method; default is 'Ascii'",
        choices=ENCODING_SCHEME_NAMES
    )
    parser.add_argument(
        '-v', '--version', action='version',
        version='%(prog)s ' + pylibdmtx.__version__
    )
    args = parser.parse_args(args)

    from PIL import Image

    encoded = encode(
        args.data.encode('utf-8'), size=args.size, scheme=args.scheme
    )
    im = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
    im.save(args.file)


if __name__ == '__main__':
    main()