def delete(self, idx: int) -> [int, None]: image = ImageModel.get_or_none(id=idx) if image is None: return ImageModel.delete().where(ImageModel.id == idx).execute() os.remove(image.path) return image.id
def style_qlssh_is(): """ 青山绿水图保留原色 @param image:base64编码后的图片字符串,如data:image/jpg;base64,/9j/4AAQSkZJRgABAQ... @param alpha:参数 @return code(200=正常返回,400=错误),url:图片地址 """ param = request.get_json() img_base64 = param.get('image') alpha = float(param.get('alpha')) user_id = param.get('id') if not all([img_base64, alpha]): return jsonify(code=400, msg='lack of parameters') img_path = utils.base64_to_imagefile(img_base64, save_path=INPUT_PATH) output_img_url = using_model(img_path, STYLE_QLSSH_PATH, alpha, True) img_url = HOST + '/' + str(output_img_url) if not user_id: return jsonify(url=img_url, code=200, msg='success without id') try: image = Image(url=img_url) db.session.add(image) db.session.flush() t = int(time.time()) relation = Transfer(user_id=user_id, image_id=image.id, timestamp=str(t)) db.session.add(relation) db.session.commit() return jsonify(url=img_url, code=200, msg='success with id') except Exception as e: return jsonify(url=img_url, code=500, msg='database error')
def add_data(idx: int, data: dict) -> [int, None]: image = ImageModel.get_or_none(id=idx) if image is None: return image.data = data image.save() return image.id
def add( self, extension: str, content_type: str, image_obj: BinaryIO, image_name: Union[str, Path] = None, ) -> int: image_dir = self.__make_image_dir() image_path = Path(image_dir, str(uuid4())).with_suffix(f'.{extension}') # get vector from image vector = self.image_to_vec.get_vector(image_obj) # save in db image = ImageModel.create( name=image_name, content_type=content_type, path=image_path.as_posix(), vector=vector.tolist(), ) image.save() # save in fs with open(image_path, 'wb') as f: copyfileobj(image_obj, f) return image.id
def search(self, image_obj: BinaryIO, k: int = 10, query: dict = None) -> [List[Dict], None]: vector = self.image_to_vec.get_vector(image_obj) res = ImageModel.search(vector=vector.tolist(), query=query, k=k) return res
def get_data(idx) -> [dict, None]: image = ImageModel.get_or_none(id=idx) if image is None: return result = { 'id': image.id, 'name': image.name, 'content_type': image.content_type, 'path': image.path, 'data': image.data, 'vector': image.vector } return result
def get_data_query(query): res = ImageModel.get_by_query(query) return res
def get(idx: int) -> [dict, None]: image = ImageModel.get_or_none(id=idx) if image: return model_to_dict(image) return