File size: 2,502 Bytes
117f0b0
 
 
 
 
 
ba78ec2
 
 
 
 
 
 
117f0b0
 
 
 
 
 
 
ba78ec2
 
 
117f0b0
 
 
 
 
 
 
 
 
ba78ec2
 
 
117f0b0
 
 
 
 
 
ba78ec2
 
 
117f0b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import qrcode as qr
import base64
import cv2
import os
from PIL import Image

def make_qr(txt=None,data=None,im_size=None,color_f=None,color_b=None):
        
    qrm = qr.QRCode(box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
    if color_f == None:
        color_f = "#000"
    if color_b == None:
        color_b = "#fff"
    if txt != None and txt != "" and data != None:
        f = Image.open(f'{data}')
        f.thumbnail((im_size,im_size))
        f.save("tmp.jpg")
        imr = open(f'tmp.jpg','rb')
        out = f'{txt}+++{base64.b64encode(imr.read())}'
        print (f'txt+data {out}')
        qrm.add_data(out)
        qrm.make(fit=True)
        img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
        img1.save("im.png")
        return "im.png"
    if txt == None or txt == "" and data != None:
        f = Image.open(f'{data}')
        f.thumbnail((im_size,im_size))
        f.save("tmp1.jpg")
        imr = open(f'tmp1.jpg','rb')
        out = f'+++{base64.b64encode(imr.read())}'
        print (f'data {out}')
        qrm.add_data(out)
        qrm.make(fit=True)
        img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
        img1.save("im1.png")
        return "im1.png"
        
    if txt != None and txt != "" and data == None:
        out = f'{txt}'
        print (f'txt {out}')
        qrm.add_data(out)
        qrm.make(fit=True)
        img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
        img1.save("im2.png")
        return "im2.png"    

def cnt_im_bytes(im,txt_cnt,im_size):
    f = Image.open(f'{im}')
    f.thumbnail((im_size,im_size))
    f.save("tmp11.jpg")
    im_cnt=os.stat('tmp11.jpg').st_size
    print(im_cnt)
    tot_cnt=im_cnt+int(txt_cnt)
    return im_cnt,tot_cnt  

def cnt_bytes(txt,im_cnt):
    txt_cnt = (len(txt.encode('utf-8')))
    tot_cnt = txt_cnt + int(im_cnt)
    return txt_cnt, tot_cnt


def decode(im):
    
    image = cv2.imread(f'{im}')
    qrCodeDetector = cv2.QRCodeDetector()
    decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
    if points is not None:
        text = decodedText
    else:
        text = "No QR Code Found"
    return text
def make_im(tx_str):
    out = tx_str.split("+++b",1)[1]
    out.replace("'","")
    print(out)
    decoded_data=base64.b64decode((out))
    
    #write the decoded data back to original format in  file
    img_file = open('image.jpeg', 'wb')
    img_file.write(decoded_data)
    img_file.close()
    return ('image.jpeg')