Spaces:
Build error
Build error
| import gradio as gr | |
| import qrcode as qr | |
| import base64 | |
| import cv2 | |
| import os | |
| from PIL import Image | |
| import zxingcpp | |
| def init_fn(txt=None,fill=None,back=None): | |
| data = 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,fit=True,box_size=1000,error_correction=qr.constants.ERROR_CORRECT_H) | |
| img1 = qr.make(out) | |
| 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'+++{str(base64.b64encode(imr.read()))}' | |
| print (f'data {out}') | |
| img1 = qr.make(str(out)) | |
| img1.save("im1.png") | |
| return "im1.png" | |
| if txt != None and txt != "" and data == None: | |
| out = f'{txt}' | |
| print (f'txt {out}') | |
| qrm = qr.QRCode() | |
| qrm.add_data(out) | |
| qrm.make(fit=True) | |
| img1 = qrm.make_image(fill_color=fill, back_color=back) | |
| 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 decode1(im): | |
| try: | |
| img = cv2.imread(f'{im}') | |
| results = zxingcpp.read_barcodes(img) | |
| print (results[0].text) | |
| return results[0].text | |
| except Exception: | |
| text = "No QR Code Found" | |
| print ("None Detected") | |
| 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') | |
| ''' | |
| BEGIN:VCARD | |
| VERSION:4.0 | |
| EMAIL;type=WORK:[email protected] | |
| EMAIL;type=HOME:[email protected] | |
| TEL;type=WORK:+1 (615) 669-9734 | |
| NOTE:here's some random text to throw into the contact information | |
| ADR;type=WORK:;;123 fake street;Nashville;TN;37228;United States of America | |
| CATEGORIES: blogger, internet troll | |
| GENDER:M | |
| PHOTO;JPEG:https://secure.gravatar.com/avatar/4fdde8e771f209d9a50ceb0f02ba60b8?s=100&d=retro&r=pg | |
| LOGO;JPEG:https://pocketables.com/wp-content/uploads/2010/05/Pocketables_logo_400x400.jpg | |
| TZ:America/Chicago | |
| URL:https://www.pocketables.com | |
| FN:Paul E. King | |
| N:Paul King | |
| END:VCARD | |
| ''' | |
| vCARD_data = { | |
| "Full Name": "FN:", | |
| "URL": "URL:", | |
| "Work Email": "EMAIL;type=WORK:", | |
| "Email Home": "EMAIL;type=HOME:", | |
| "Telephone Work": "TEL;type=WORK:", | |
| "Telephone Cell": "TEL;type=CELL:", | |
| "Address Work": "ADR;type=WORK:", | |
| "Note": "NOTE:", | |
| "Categories": "CATEGORIES:", | |
| "Photo (URL)": "PHOTO;JPEG:", | |
| "Logo (URL)": "LOGO;JPEG:", | |
| "Timezone": "TZ:", | |
| } | |
| def test_fn(fill, back, v_data_0,v_data_1,v_data_2,v_data_3,v_data_4,v_data_5,v_data_6,v_data_7,v_data_8,v_data_9,v_data_10,v_data_11): | |
| out_vcard = """BEGIN:VCARD | |
| VERSION:4.0""" | |
| if v_data_0 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[0]}{v_data_0}" | |
| if v_data_1 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[1]}{v_data_1}" | |
| if v_data_2 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[2]}{v_data_2}" | |
| if v_data_3 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[3]}{v_data_3}" | |
| if v_data_4 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[4]}{v_data_4}" | |
| if v_data_5 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[5]}{v_data_5}" | |
| if v_data_6 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[6]}{v_data_6}" | |
| if v_data_7 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[7]}{v_data_7}" | |
| if v_data_8 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[8]}{v_data_8}" | |
| if v_data_9 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[9]}{v_data_9}" | |
| if v_data_10 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[10]}{v_data_10}" | |
| if v_data_11 != "": | |
| out_vcard = f"{out_vcard}\n{list(vCARD_data.values())[11]}{v_data_11}" | |
| out_vcard = f"{out_vcard}\nEND:VCARD" | |
| try: | |
| out_qr = init_fn(out_vcard, fill, back) | |
| except Exception: | |
| out_qr = None | |
| return out_vcard, out_qr | |
| with gr.Blocks() as app: | |
| with gr.Row(): | |
| with gr.Column(): | |
| with gr.Box(): | |
| v_data_0 = gr.Textbox(label = list(vCARD_data)[0]) | |
| v_data_1 = gr.Textbox(label = list(vCARD_data)[1]) | |
| v_data_2 = gr.Textbox(label = list(vCARD_data)[2]) | |
| v_data_3 = gr.Textbox(label = list(vCARD_data)[3]) | |
| v_data_4 = gr.Textbox(label = list(vCARD_data)[4]) | |
| v_data_5 = gr.Textbox(label = list(vCARD_data)[5]) | |
| v_data_6 = gr.Textbox(label = list(vCARD_data)[6]) | |
| v_data_7 = gr.Textbox(label = list(vCARD_data)[7]) | |
| v_data_8 = gr.Textbox(label = list(vCARD_data)[8]) | |
| v_data_9 = gr.Textbox(label = list(vCARD_data)[9]) | |
| v_data_10 = gr.Textbox(label = list(vCARD_data)[10]) | |
| v_data_11 = gr.Textbox(label = list(vCARD_data)[11]) | |
| txt_box = gr.Textbox() | |
| init_btn = gr.Button("Make QR") | |
| #data_box=gr.Image(type='filepath') | |
| #im_size=gr.Slider(10,100,step=1,value=20) | |
| with gr.Row(): | |
| fill = gr.ColorPicker(label = "Fill Color", value="#000000") | |
| back = gr.ColorPicker(label = "Background Color", value="#FFFFFF") | |
| tot_cnt=gr.Textbox(label="Bytes (2000 max)",visible=True) | |
| with gr.Column(): | |
| qr_out = gr.Image(type='filepath') | |
| dec_btn = gr.Button("Decode QR") | |
| text_box=gr.Textbox(label='Decoded Text') | |
| #dec_im=gr.Image() | |
| with gr.Row(visible=False): | |
| txt_cnt=gr.Textbox(value=0) | |
| im_cnt=gr.Textbox(value=0) | |
| init_btn.click(test_fn, [fill, back, v_data_0,v_data_1,v_data_2,v_data_3,v_data_4,v_data_5,v_data_6,v_data_7,v_data_8,v_data_9,v_data_10,v_data_11], [txt_box,qr_out]) | |
| #im_size.change(cnt_im_bytes,[data_box,txt_cnt,im_size],[im_cnt,tot_cnt],show_progress=False) | |
| #data_box.change(cnt_im_bytes,[data_box,txt_cnt,im_size],[im_cnt,tot_cnt],show_progress=False) | |
| txt_box.change(cnt_bytes,[txt_box,im_cnt],[txt_cnt,tot_cnt],show_progress=False) | |
| #text_box.change(make_im,text_box,dec_im) | |
| dec_btn.click(decode1,qr_out,text_box) | |
| ##init_btn.click(init_fn,[txt_box,fill,back],qr_out) | |
| app.queue(concurrency_count=10).launch() |