-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchepy_qr.py
98 lines (84 loc) · 2.86 KB
/
chepy_qr.py
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
import logging
import tempfile
import chepy.core
try:
from PIL import Image
from pyzbar import pyzbar
import zxing
except ImportError:
logging.error("Could not import PIL, zxing or pyzbar")
class Chepy_QR(chepy.core.ChepyCore):
pass
@chepy.core.ChepyDecorators.call_stack
def qr_decode(self, show_all: bool = False): # pragma: no cover
"""Decode a qr code. This method does require zbar to be installed in the system
Args:
show_all: If true, show all decoded data. If false, show only the first decoded data.
Returns:
ChepyPlugin: The Chepy object.
"""
data = Image.open(self._load_as_file())
texts = list(map(lambda x: x.data, pyzbar.decode(data)))
if len(texts) == 0:
logging.error("Could not decode QR code")
else:
if show_all:
self.state = texts
else:
self.state = texts[0]
return self
@chepy.core.ChepyDecorators.call_stack
def qr_decode_other(self):
"""Decode a qr code
This method does require zxing to be installed. For this method to work,
it creates a temporary file and then deletes it.
Returns:
ChepyPlugin: The Chepy object.
"""
formats = {"PNG": "png", "JPEG": "jpg", "GIF": "gif"}
data = Image.open(self._load_as_file())
fmat = formats.get(data.format)
if fmat is None:
logging.error("Could not decode QR code")
return self
# create temp file
with tempfile.NamedTemporaryFile(delete=True, suffix=".{}".format(fmat)) as tf:
data.save(tf)
# tf.write(data.tobytes())
reader = zxing.BarCodeReader()
result = reader.decode(tf.name)
if result is None:
logging.error("Could not decode QR code")
else:
self.state = result.parsed
return self
# @chepy.core.ChepyDecorators.call_stack
# def qr_to_ascii(self):
# """
# Convert a qr code to ascii
# Returns:
# ChepyPlugin: The Chepy object.
# """
# # TODO: Implement this
# logging.warning("Not implemented yet")
# pass
# img = img.convert("1")
# h, w = img.size
# art_width: int = 64
# aspect_ratio = h / w
# new_width = art_width
# new_height = aspect_ratio * new_width
# img = img.resize((new_width, int(new_height)))
# # print(img)
# # pixels = img.getdata()
# lines = []
# for y in range(0, new_width):
# hold = []
# for x in range(0, int(new_height)):
# pixel = img.getpixel((x,y,))
# if pixel > 200:
# hold.append('█')
# else:
# hold.append(' ')
# lines.append(''.join(hold))
# print('\n'.join(lines))