Last active
June 5, 2017 17:43
-
-
Save yrps/b8a0d5c5b6da3a045c540184f27d22ce to your computer and use it in GitHub Desktop.
A trivial QR encoder and decoder.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
def encode(content, output, scale): | |
import pyqrcode | |
qr = pyqrcode.create(content) | |
qr.png(output, scale) | |
def decode(file_path): | |
import PIL | |
import zbarlight | |
with open(file_path, 'rb') as image_file: | |
image = PIL.Image.open(image_file) | |
image.load() | |
for code in zbarlight.scan_codes('qrcode', image): | |
if isinstance(code, bytes): | |
print(code.decode()) | |
else: | |
print(code) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description=''' | |
A trivial QR encoder and decoder.''', epilog=''' | |
Encoding requires the pyqrcode and pypng modules. | |
Decoding requires the PIL and zbarlight modules. | |
The zbarlight module requires the zbar or zbar-headless system package. | |
https://pypi.python.org/pypi/PyQRCode/ | |
https://pypi.python.org/pypi/zbarlight/ | |
http://zbar.sourceforge.net/ | |
''', formatter_class=argparse.RawDescriptionHelpFormatter) | |
pgroup = parser.add_mutually_exclusive_group() | |
pgroup.add_argument('-e', '--encode', help='encode content to QR image', | |
action='store_true') | |
pgroup.add_argument('-d', '--decode', action='store_true', default=True, | |
help='decode a QR image to content (default)') | |
parser.add_argument('-o', '--out', default='qr.png', | |
help='name of output png file (default: %(default)s)') | |
parser.add_argument('-s', '--scale', type=int, default=8, | |
help='scale of encoded file (default: %(default)d)') | |
parser.add_argument('file_path_or_content', | |
help='file to be decoded or content to be encoded') | |
args = parser.parse_args() | |
if args.encode: | |
encode(args.file_path_or_content, args.out, args.scale) | |
elif args.decode: | |
decode(args.file_path_or_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussion:
https://ncoop.github.io/2017/06/03/miniqr-py-a-simple-qr-code-cli