-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
53 lines (37 loc) · 1.33 KB
/
app.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
import qrcode
import numpy as np
from flask import Flask
app = Flask(__name__)
DEFAULT_TEXT = "https://github.com/D-Chandrahas/encodeQR"
@app.route("/", methods=["GET"])
@app.route("/<path:text>", methods=["GET"])
def return_qr(text = DEFAULT_TEXT):
# generate qr code as binary matrix
qr = qrcode.QRCode(
version=None,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=1,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
qr = qr.make_image(fill_color="black", back_color="white")
qr = np.array(qr)
# if height of qr code is odd, append one row of zeros
# since each block character is 2x1 (height x width)
if (qr.shape[0]%2 == 1):
qr = np.vstack((qr, np.zeros((1, qr.shape[1]), dtype=bool)))
res_str = []
# print the qr code using block unicode chars ('▄','█','▀',' ')
for i in range(0, qr.shape[0], 2):
for j in range(qr.shape[1]):
if qr[i,j] == True and qr[i+1,j] == True:
res_str.append("█")
elif qr[i,j] == True and qr[i+1,j] == False:
res_str.append("▀")
elif qr[i,j] == False and qr[i+1,j] == True:
res_str.append("▄")
else:
res_str.append(" ")
res_str.append("\n")
return "".join(res_str)