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