def pdf(): print(request.method) if request.method == 'POST': print(request.files) if 'file' not in request.files: print('no file') return redirect(request.url) file = request.files['file'] if file and allowed_file(file.filename): filename = file.filename file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) print(filename.replace(".pdf", ".jpg")) print(app.config['UPLOAD_FOLDER']) converter_pdf(app.config['UPLOAD_FOLDER'], filename, app.config['UPLOAD_FOLDER'], filename.replace(".pdf", ".jpg"), 50) print("file has been uploaded") print("eee") return render_template('crop_first_img.html', msg=str(filename.replace(".pdf", ".jpg"))) return 'a string' return "Can't post file"
def check_file(db, url_path): pdffilename = url_path.split("/")[-1] path_source = "static/queues_pdf/" path_src = url_path path_destination = "static/check_convert_300/" file_300 = "static/check_convert_300/" file_200 = "static/check_convert_200/" file_name = pdffilename.replace(".pdf", ".jpg") alpha = 7 converter_pdf(path_source, pdffilename, file_300, file_name, 300) resizer_img(file_300, file_name, file_200, file_name, alpha) file_name_img = file_200 + file_name image = cv2.imread(file_name_img) # db path_main = "./static/" rect0, name0 = found_rectlogo(db, path_main, image, file_name, alpha) # cv2.imwrite("0000.jpg", rect0) # db text_extract = get_data(db, path_main, rect0, name0, url_path) result = db.query( models.PdfInfo).filter(models.PdfInfo.url == url_path).first() result.extracted_data = text_extract result.status = "Done" db.commit() # db return text_extract
async def create_file(file: UploadFile = File(...), current_user: User = Depends(get_current_active_user)): global upload_folder upload_folder = ".//static//template_pdf" if '.pdf' in file.filename: file_object = file.file user_id = current_user.full_name rand_letters = ''.join(random.choices(letters, k=5)) filename = file.filename.replace( '.pdf', '') + '___' + str(user_id) + '_' + rand_letters + '.pdf' #create empty file to copy the file_object to upload_folder = open(os.path.join(upload_folder, filename), 'wb+') shutil.copyfileobj(file_object, upload_folder) upload_folder.close() # elif '.txt' in file.filename: # upload_folder = open(os.path.join(upload_folder, file.filename), 'wb+') # shutil.copyfileobj(file_object, upload_folder) # upload_folder.close() # list_urls=list(np.loadtxt(demostring, delimiter =';', unpack = True)) # for url in urls: # if "http"==file.filename[:4] or "ftp" == file.filename[:3]: # filename = wget.download(url,out="./check") # url_path=filename path_source = "//static//template_pdf//" # path_src=url_path file_300 = "static/template_img_300/" file_200 = "static/template_img_200/" imgfilename = (filename).replace(".pdf", ".jpg") pdffilename = filename alpha = 7 converter_pdf(path_source, pdffilename, file_300, imgfilename, 300) resizer_img(os.path.join(os.getcwd(), file_300), imgfilename, os.path.join(os.getcwd(), file_200), imgfilename, alpha) # converter_pdf(path_source,pdffilename,file_200,imgfilename,200) try: name = ".".join(filename.split(".")[:-1]) except: name = filename.split(".")[:-1] template = schemas.TemplateCreate(name=name, crop_json=None, status_crop=False, label_json=None, label_status=False, template_path=(filename).replace( ".pdf", ".jpg")) crud.create_template(db=db, item=template, user_id="1") return {"filename": filename}
def check_file(app, request): if request.method == 'POST': # if 'file' not in request.files: # print ('no file') file = request.files['file'] if file and allowed_file(file.filename): file_name = file.filename path = os.path.join(app.config['UPLOAD_FOLDER'], "check/") print(path, file=sys.stdout) path_src = os.path.join(app.config['UPLOAD_FOLDER'], "check/", file_name) path_dst = os.path.join(app.config['UPLOAD_FOLDER'], "check_convert_300/") file.save(path_src) file_300 = os.path.join(app.config['UPLOAD_FOLDER'], "check_convert_300/") file_200 = os.path.join(app.config['UPLOAD_FOLDER'], "check_convert_200/") filename = file_name.replace(".pdf", ".jpg") converter_pdf(app.config['UPLOAD_FOLDER'], file_name, file_300, filename, 300) resizer_img(file_300, filename, file_200, filename, 7) file_name_img = file_200 + filename img_rgb = cv2.imread(file_name_img) alpha = 7 rect0, name0 = found_rectlogo(app.config['UPLOAD_FOLDER'], img_rgb, filename, alpha) cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], "0000.jpg"), rect0) msg = get_data(app.config['UPLOAD_FOLDER'], rect0, name0) import json msg = json.loads(msg) title = msg['text'][0]["Title"] drawing = msg['text'][0]["drawingN"] revsion = msg['text'][0]["revsion"] # try: # msg=process_image(app.config['UPLOAD_FOLDER'],file_name,path_dst) return render_template('json_tmp.html', title=title, drawingN=drawing, revsion=revsion)
def upload_convert_pdf(app, request): print(request) if request.method == 'POST': if 'file' not in request.files: print('no file') return redirect(request.url) file = request.files['file'] if file and allowed_file(file.filename): file_name = file.filename file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name)) file_300 = os.path.join(app.config['UPLOAD_FOLDER'], "img300/") file_200 = os.path.join(app.config['UPLOAD_FOLDER'], "img200/") filename = file_name.replace(".pdf", ".jpg") converter_pdf(app.config['UPLOAD_FOLDER'], file_name, file_300, filename, 300) resizer_img(file_300, filename, file_200, filename, 7) return filename return "Can't post file"