Spaces:
Running
Running
import qrcode as qr | |
import base64 | |
import cv2 | |
import os | |
from PIL import Image | |
def make_qr(txt=None,data=None,im_size=None): | |
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}') | |
img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H) | |
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}') | |
img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H) | |
img1.save("im1.png") | |
return "im1.png" | |
if txt != None and txt != "" and data == None: | |
out = f'{txt}' | |
print (f'txt {out}') | |
img1 = qr.make(out,box_size=10,error_correction=qr.constants.ERROR_CORRECT_H) | |
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') |