-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr_code.py
46 lines (36 loc) · 1.52 KB
/
qr_code.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
import base64
import io
import qrcode
from payment_info import PaymentInfo
PAYMENT_CODE_TEMPLATE = r"SPD*1.0*ACC:{acc_number}*AM:{amount_czk:.2f}*CC:CZK" \
r"*MSG:{message}*X-VS:{vs}*X-SS:{ss}"
def qr_platba_string(pi: PaymentInfo):
"""Format from https://qr-platba.cz/"""
code = PAYMENT_CODE_TEMPLATE.format(message=pi.payment_message,
vs=pi.variable_symbol,
ss=pi.specific_symbol,
acc_number=pi.iban_account_number,
amount_czk=float(pi.amount_czk))
return code
class QRCode:
def __init__(self, code):
self.img = qrcode.make(code)
# type(img) # qrcode.image.pil.PilImage
def save(self, filename):
self.img.save(f"{filename}.png")
def get_image_bytes(self):
# Save to memory buffer
in_mem_file = io.BytesIO()
self.img.save(in_mem_file)
# reset file pointer to start
in_mem_file.seek(0)
img_bytes = in_mem_file.read()
return img_bytes
def base64str(self):
"""
Convert image to base64 string, as used in the email template.
from https://stackoverflow.com/questions/42503995/how-to-get-a-pil-image-as-a-base64-encoded-string
"""
base64_encoded_result_bytes = base64.b64encode(self.get_image_bytes())
base64_encoded_result_str = base64_encoded_result_bytes.decode('ascii')
return base64_encoded_result_str