def post(self): form = ImageForm() if form.validate_on_submit(): image = form.image.data.stream.read() string_image = f'data:{form.image.data.mimetype};base64,' + base64.b64encode(image).decode('utf-8') url = f"{os.getenv('IMG_SERVER')}/" data = { 'user_id': current_user.id, 'title': form.data.get('title'), 'image': string_image } response = AuthenticatedRequest.make(url, method=RequestMethod.POST, json=data) if response.status_code == 200: flash(f"Image {data['title']} successfully uploaded!") session['uploaded_image'] = response.json()['image'] session['display_counter'] = 0 return redirect(url_for('image')) return 'Something went wrong'
def index(): error = None form = ImageForm(request.form) if request.method == "POST": if form.validate_on_submit(): image = request.files.get('image') #print image filename = None #title = str(image.filename).rsplit('.',1)[0] if image: # postpic = PostPic(None) # db.session.add(postpic) # db.session.flush() # import pdb; pdb.set_trace() # print postpic.id try: #filename = upload_images.save(image, folder=str(postpic.id)) filename = upload_images.save(image) except: flash("The images was not uploaded") if filename: #print image.url link = random_generator().lower() postpic=PostPic(filename,link) db.session.add(postpic) db.session.commit() return redirect(url_for('show', path_id=postpic.link)) return render_template('index.html', form=form, error=error)
def edit_image(id): member = Member( ) #this is the member object from which the id is to be searched and then set the image_file to a new value form = ImageForm( ) #This is the member form that doesnt use a request method qry = db.session.query(Member).filter( Member.id == id) #Id from the invoking object member = qry.first() image = member.image_file #Get the image_file from the database and check whether the image has been uploaded or not if image == "noavatar92": #Incase the user image is not yet uploaded image_file = url_for( 'static', filename='img/' + member.image_file + '.png' ) #Manually add the extension to the image file from the database else: image_file = url_for( 'static', filename='img/' + member.image_file) #when a user image was initially uploaded if form.validate_on_submit(): filename = photos.save(form.photo.data) file_url = photos.url(filename) f_name, f_ext = os.path.splitext(filename) picture_fn = f_name + f_ext picture_path = os.path.join(app.root_path, 'static/profile_pics', picture_fn) member.image_file = picture_fn #Dont forget to change the image path if it workd later on db.session.commit() else: file_url = None return render_template('edit-image.html', form=form, file_url=file_url, image_file=image_file)
def OCR_Process(): form = ImageForm() if form.validate_on_submit(): page_num = form.pageNum.data page_num_lst = [] j = "" for i in page_num: if (i != ","): j = j + i else: page_num_lst.append(j) j = "" page_num_lst.append(j) print(page_num_lst) upload_folder = "C:/Project Spreading tool/OCR_APP/tmp/" for i in page_num_lst: filename = "split-page%s.pdf" % i filename1 = "split-page%s.png" % i image_conversion(upload_folder, filename, i) OCR_read1(upload_folder, filename, i) #page_num_lst = ','.join(page_num_lst1) return redirect(url_for('FinancialRisk', page_num_lst=page_num_lst), code=307) return render_template('OCR_Process.html', form=form)
def upload(): form = ImageForm() if form.validate_on_submit(): filename = secure_filename(form.image.data.filename) form.image.data.save('project/static/images/' + filename) flash('New image was successfully posted. Thanks.') else: filename = None return render_template('upload.html', form=form, filename=filename)
def test(): form = ImageForm() image = Image() if form.validate_on_submit(): if form.photo.data: picture_file = save_picture(form.photo.data) image.image_file = picture_file image.name = form.name.data db.session.add(image) db.session.commit() #image_file = url_for('static', filename='img' + image_f) return render_template('image.html', form=form)
def carousel(): form = ImageForm() if form.validate_on_submit(): i = form.image.data filename = secure_filename(i.filename) with open('static/img/carousel/' + filename, 'wb') as f: f.write(i.read()) app.config['IMAGES'] += 1 return redirect('carousel') images = os.listdir('static/img/carousel/') return render_template('carousel.html', images=[ url_for('static', filename='img/carousel/' + image) for image in images ], form=form)
def show_edit_image(image_id): """Show the edit image form and edit the image in the database.""" if not g.user: return render_template("home-anon.html") image = Image.query.get_or_404(image_id) form = ImageForm(obj=image) if form.validate_on_submit(): image.image = form.image.data image.title = form.title.data image.caption = form.caption.data image.is_private = form.is_private.data db.session.commit() flash("Image updated.", "success") return redirect(f"/shops/{image.shop}") return render_template("images/edit-image.html", form=form)
def upload_file(): image = Image() form = ImageForm( ) #The request method brings about the frontend error in the template file ie "No file was chosen" if form.validate_on_submit(): filename = photos.save(form.photo.data) file_url = photos.url(filename) f_name, f_ext = os.path.splitext(filename) picture_fn = f_name + f_ext picture_path = os.path.join(app.root_path, 'static/profile_pics', picture_fn) image.image_file = picture_fn #Dont forget to change the image path if it workd later on image.name = form.name.data db.session.add(image) db.session.commit() else: file_url = None return render_template('image_final.html', form=form, file_url=file_url)
def edit(): image = Image() # form = ImageForm() qry = db.session.query(Image).filter(Image.name.contains("oben")) image_fn = qry.first() #result from the query image_file = url_for('static', filename='img/' + image_fn.image_file) # if form.validate_on_submit(): filename = photos.save(form.photo.data) file_url = photos.url(filename) f_name, f_ext = os.path.splitext(filename) picture_fn = f_name + f_ext picture_path = os.path.join(app.root_path, 'static/profile_pics', picture_fn) image_fn.image_file = picture_fn #Dont forget to change the image path if it workd later on #image_fn.name = form.name.data #db.session.add(image) db.session.commit() else: file_url = None return render_template('edit-image.html', form=form, file_url=file_url, image_file=image_file)