Omnibus commited on
Commit
b2b763e
1 Parent(s): 71c2a52

Create qr.py

Browse files
Files changed (1) hide show
  1. qr.py +72 -0
qr.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import qrcode as qr
3
+ import base64
4
+ import cv2
5
+ import os
6
+ from PIL import Image
7
+
8
+ def make_qr(txt=None,data=None,im_size=None):
9
+ if txt != None and txt != "" and data != None:
10
+ f = Image.open(f'{data}')
11
+ f.thumbnail((im_size,im_size))
12
+ f.save("tmp.jpg")
13
+ imr = open(f'tmp.jpg','rb')
14
+ out = f'{txt}+++{base64.b64encode(imr.read())}'
15
+ print (f'txt+data {out}')
16
+ img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
17
+ img1.save("im.png")
18
+ return "im.png"
19
+ if txt == None or txt == "" and data != None:
20
+ f = Image.open(f'{data}')
21
+ f.thumbnail((im_size,im_size))
22
+ f.save("tmp1.jpg")
23
+ imr = open(f'tmp1.jpg','rb')
24
+ out = f'+++{base64.b64encode(imr.read())}'
25
+ print (f'data {out}')
26
+ img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
27
+ img1.save("im1.png")
28
+ return "im1.png"
29
+
30
+ if txt != None and txt != "" and data == None:
31
+ out = f'{txt}'
32
+ print (f'txt {out}')
33
+ img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
34
+ img1.save("im2.png")
35
+ return "im2.png"
36
+
37
+ def cnt_im_bytes(im,txt_cnt,im_size):
38
+ f = Image.open(f'{im}')
39
+ f.thumbnail((im_size,im_size))
40
+ f.save("tmp11.jpg")
41
+ im_cnt=os.stat('tmp11.jpg').st_size
42
+ print(im_cnt)
43
+ tot_cnt=im_cnt+int(txt_cnt)
44
+ return im_cnt,tot_cnt
45
+
46
+ def cnt_bytes(txt,im_cnt):
47
+ txt_cnt = (len(txt.encode('utf-8')))
48
+ tot_cnt = txt_cnt + int(im_cnt)
49
+ return txt_cnt, tot_cnt
50
+
51
+
52
+ def decode(im):
53
+
54
+ image = cv2.imread(f'{im}')
55
+ qrCodeDetector = cv2.QRCodeDetector()
56
+ decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
57
+ if points is not None:
58
+ text = decodedText
59
+ else:
60
+ text = "No QR Code Found"
61
+ return text
62
+ def make_im(tx_str):
63
+ out = tx_str.split("+++b",1)[1]
64
+ out.replace("'","")
65
+ print(out)
66
+ decoded_data=base64.b64decode((out))
67
+
68
+ #write the decoded data back to original format in file
69
+ img_file = open('image.jpeg', 'wb')
70
+ img_file.write(decoded_data)
71
+ img_file.close()
72
+ return ('image.jpeg')