def upload(self, imgfile, siteid, user): session = db.Session() site = db.Site.get(session, int(siteid)) if siteid else None by = db.Person.get(session, user) if user else None #io = StringIO() # for l in imgfile: # io.write(l) # io.seek(0) img = db.Image(site=site, by=by, imagefile=imgfile.file) session.add(img) session.commit() imgid = img.id session.close() raise web.HTTPRedirect('/picture?id=%i' % imgid)
def handler(event, context): f = event["extensions"]["request"].files.file.file imagedata = BytesIO(f.read()) thumbdata = BytesIO() fmt = None with Image.open(imagedata) as image: fmt = image.format.lower().replace("e", "") if not fmt in ("bmp", "jpg", "png"): return bottle.HTTPResponse( status=400, body=json.dumps({"error": "invalid image format: " + fmt})) thumb = image.copy() thumb.thumbnail((300, 60), Image.LANCZOS) thumb.save(thumbdata, format=image.format) imagepath = hashlib.sha1(imagedata.getvalue()).hexdigest().upper() thumbpath = hashlib.sha1(thumbdata.getvalue()).hexdigest().upper() imagepath = imagepath[0:2] + "/" + imagepath[2:4] + "/" + imagepath[4:] thumbpath = thumbpath[0:2] + "/" + thumbpath[2:4] + "/" + thumbpath[4:] s3 = boto3.session.Session().resource( "s3", region_name="sfo2", endpoint_url="https://sfo2.digitaloceanspaces.com", aws_access_key_id=access_key, aws_secret_access_key=access_secret, ) s3.Object("diff-pics", "images/" + imagepath).put(Body=imagedata.getvalue(), ACL="public-read", ContentType="image/" + fmt) s3.Object("diff-pics", "thumbs/" + thumbpath).put(Body=thumbdata.getvalue(), ACL="public-read", ContentType="image/" + fmt) db.session.add(db.Image(path=imagepath, thumb=thumbpath, format=fmt)) db.session.commit() return json.dumps({"image": imagepath, "thumb": thumbpath})
if choice == 'Raw data': st.title('Show raw data') if choice == 'Upload': st.title('Uploading image') imgs = st.file_uploader("Choose a image to upload",type = ('JPG','PNG'),accept_multiple_files=True) for imgdata in imgs: if imgdata: # load image as a Pillow object im = Image.open(imgdata) # create a address for image path path = os.path.join(UPLOAD_FOLDER,imgdata.name) # save file to upload folder im.save(path,format=imgdata.type.split('/')[1]) # saves info to db sess = open_db() imdb = db.Image(path=path) sess.add(imdb) sess.commit() sess.close() # show a msg st.success('image uploaded successfully') if choice == 'Enhance Resolution': st.title('Enhance Resolution') sess = open_db() images = sess.query(db.Image).all() sess.close() select_img = st.sidebar.radio("select an image",images) if select_img: im = Image.open(select_img.path) st.image(im,use_column_width=True)