| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import argparse |
| 5 |
import sys |
| 6 |
|
| 7 |
import pylibdmtx |
| 8 |
from pylibdmtx.pylibdmtx import ( |
| 9 |
encode, ENCODING_SIZE_NAMES, ENCODING_SCHEME_NAMES |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
def main(args=None): |
| 14 |
if args is None: |
| 15 |
args = sys.argv[1:] |
| 16 |
|
| 17 |
parser = argparse.ArgumentParser( |
| 18 |
description='Writes a datamatrix barcode to a new image file' |
| 19 |
) |
| 20 |
parser.add_argument('file', help='Filename of the output image') |
| 21 |
parser.add_argument( |
| 22 |
'data', help='Data to be written; will be utf-8 encoded' |
| 23 |
) |
| 24 |
parser.add_argument( |
| 25 |
'--size', |
| 26 |
help="Encoding size (not image dimensions); default is 'ShapeAuto'", |
| 27 |
choices=ENCODING_SIZE_NAMES |
| 28 |
) |
| 29 |
parser.add_argument( |
| 30 |
'--scheme', |
| 31 |
help="Encoding method; default is 'Ascii'", |
| 32 |
choices=ENCODING_SCHEME_NAMES |
| 33 |
) |
| 34 |
parser.add_argument( |
| 35 |
'-v', '--version', action='version', |
| 36 |
version='%(prog)s ' + pylibdmtx.__version__ |
| 37 |
) |
| 38 |
args = parser.parse_args(args) |
| 39 |
|
| 40 |
from PIL import Image |
| 41 |
|
| 42 |
encoded = encode( |
| 43 |
args.data.encode('utf-8'), size=args.size, scheme=args.scheme |
| 44 |
) |
| 45 |
im = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels) |
| 46 |
im.save(args.file) |
| 47 |
|
| 48 |
|
| 49 |
if __name__ == '__main__': |
| 50 |
main() |