|
@@ -6,7 +6,9 @@ import numpy as np
|
|
from .req import ReplaceForm, is_empty
|
|
from .req import ReplaceForm, is_empty
|
|
import tools
|
|
import tools
|
|
import cv2
|
|
import cv2
|
|
-from .file import get_upload_file_path, get_output_dir, file_url, get_output_file_path, get_file_id
|
|
|
|
|
|
+from .file import get_upload_file_path, get_output_dir, file_url, get_output_file_path, get_file_id, get_file_id_path
|
|
|
|
+from utils import color
|
|
|
|
+import time
|
|
|
|
|
|
log = logging.create_logger(app)
|
|
log = logging.create_logger(app)
|
|
|
|
|
|
@@ -33,23 +35,50 @@ def replace():
|
|
if form.validate() is False:
|
|
if form.validate() is False:
|
|
return jsonify(error_resp("参数错误!"))
|
|
return jsonify(error_resp("参数错误!"))
|
|
|
|
|
|
|
|
+ img_path = get_file_id_path(form.file_id.data)
|
|
|
|
+
|
|
|
|
+ exist, alpha_path, path = tools.has_seg(img_path, get_output_dir())
|
|
|
|
+
|
|
|
|
+ if exist:
|
|
|
|
+ alpha = cv2.imread(alpha_path)
|
|
|
|
+ fg = cv2.imread(path)
|
|
|
|
+ else:
|
|
|
|
+ alpha, fg, _, path = tools.seg(img_path, get_output_dir())
|
|
|
|
+
|
|
if is_empty(form.bg_file_id.data) and is_empty(form.background.data):
|
|
if is_empty(form.bg_file_id.data) and is_empty(form.background.data):
|
|
- return jsonify(error_resp("请选择需要替换的背景!"))
|
|
|
|
|
|
+ return jsonify(success_resp({
|
|
|
|
+ "fileId": form.file_id.data,
|
|
|
|
+ "url": file_url(path, True)
|
|
|
|
+ }))
|
|
|
|
|
|
- img_path = get_upload_file_path(form.file_id.data)
|
|
|
|
bg_path = form.background.data
|
|
bg_path = form.background.data
|
|
|
|
|
|
if is_empty(form.bg_file_id.data) is False:
|
|
if is_empty(form.bg_file_id.data) is False:
|
|
bg_path = get_upload_file_path(form.bg_file_id.data)
|
|
bg_path = get_upload_file_path(form.bg_file_id.data)
|
|
- if os.path.exists(bg_path):
|
|
|
|
|
|
+ if os.path.exists(bg_path) is False:
|
|
bg_path = form.background.data
|
|
bg_path = form.background.data
|
|
|
|
|
|
- result = tools.replace(img_path=img_path, background=bg_path, save_dir=get_output_dir())
|
|
|
|
|
|
+ bg = color.get_bg(bg_path, fg.shape)
|
|
|
|
+ if bg is None:
|
|
|
|
+ return jsonify(success_resp({
|
|
|
|
+ "fileId": form.file_id.data,
|
|
|
|
+ "url": file_url(path, True)
|
|
|
|
+ }))
|
|
|
|
+
|
|
|
|
+ alpha = alpha / 255.0
|
|
|
|
+ alpha = alpha[:, :, np.newaxis]
|
|
|
|
+ com = alpha * fg + (1 - alpha) * bg
|
|
|
|
+ com = com.astype('uint8')
|
|
|
|
+ filename = os.path.basename(img_path)
|
|
|
|
+ names = os.path.splitext(filename)
|
|
|
|
+ save_name = "{}_{}{}".format(names[0], time.time(), names[1])
|
|
|
|
+ com_save_path = os.path.join(get_output_dir(), save_name)
|
|
|
|
+ cv2.imwrite(com_save_path, com)
|
|
|
|
|
|
- return jsonify({
|
|
|
|
- "fileId": get_file_id(result, True),
|
|
|
|
- "url": file_url(result, True)
|
|
|
|
- })
|
|
|
|
|
|
+ return jsonify(success_resp({
|
|
|
|
+ "fileId": get_file_id(com_save_path, True),
|
|
|
|
+ "url": file_url(com_save_path, True)
|
|
|
|
+ }))
|
|
|
|
|
|
|
|
|
|
@app.route("/image/resize", methods=["POST", 'GET'])
|
|
@app.route("/image/resize", methods=["POST", 'GET'])
|
|
@@ -65,13 +94,17 @@ def resize():
|
|
rotate = request.values.get("rotate")
|
|
rotate = request.values.get("rotate")
|
|
flip = request.values.get("flip")
|
|
flip = request.values.get("flip")
|
|
rect = request.values.get('rect')
|
|
rect = request.values.get('rect')
|
|
|
|
+ save = request.values.get('onSave')
|
|
|
|
+ download = request.values.get('download')
|
|
|
|
+ save = is_empty(save) is False
|
|
|
|
+ download = is_empty(download) is False
|
|
|
|
|
|
if is_empty(reset):
|
|
if is_empty(reset):
|
|
reset = True
|
|
reset = True
|
|
else:
|
|
else:
|
|
reset = False
|
|
reset = False
|
|
|
|
|
|
- file_path = get_upload_file_path(file_id)
|
|
|
|
|
|
+ file_path = get_file_id_path(file_id)
|
|
|
|
|
|
is_get = request.method.upper() == "GET"
|
|
is_get = request.method.upper() == "GET"
|
|
|
|
|
|
@@ -86,60 +119,76 @@ def resize():
|
|
origin_h = img.shape[0]
|
|
origin_h = img.shape[0]
|
|
origin_w = img.shape[1]
|
|
origin_w = img.shape[1]
|
|
|
|
|
|
- img = crop(img, rect, origin_w, origin_h)
|
|
|
|
|
|
+ dst, r_w, r_h = crop(img, rect, origin_w, origin_h)
|
|
|
|
|
|
change_size = True
|
|
change_size = True
|
|
if is_empty(width) and is_empty(height):
|
|
if is_empty(width) and is_empty(height):
|
|
change_size = False
|
|
change_size = False
|
|
- w = origin_w
|
|
|
|
- h = origin_h
|
|
|
|
- elif is_empty(width):
|
|
|
|
- h = int(height)
|
|
|
|
- w = round(h * origin_w / origin_h)
|
|
|
|
- elif is_empty(height):
|
|
|
|
- w = int(width)
|
|
|
|
- h = round(w * origin_h / origin_w)
|
|
|
|
- else:
|
|
|
|
- w = int(width)
|
|
|
|
- h = int(height)
|
|
|
|
-
|
|
|
|
- dst = img
|
|
|
|
|
|
|
|
if change_size:
|
|
if change_size:
|
|
- dst = cv2.resize(img, (w, h))
|
|
|
|
|
|
+ if is_empty(width):
|
|
|
|
+ h = int(height)
|
|
|
|
+ w = round(h * origin_w / origin_h)
|
|
|
|
+ elif is_empty(height):
|
|
|
|
+ w = int(width)
|
|
|
|
+ h = round(w * origin_h / origin_w)
|
|
|
|
+ else:
|
|
|
|
+ w = int(width)
|
|
|
|
+ h = int(height)
|
|
|
|
+ origin = img if dst is None else dst
|
|
|
|
+ dst = cv2.resize(origin, (w, h))
|
|
|
|
+ r_w = w
|
|
|
|
+ r_h = h
|
|
|
|
+
|
|
|
|
+ if r_w is None:
|
|
|
|
+ r_w = origin_w
|
|
|
|
+ if r_h is None:
|
|
|
|
+ r_h = origin_h
|
|
|
|
|
|
if is_empty(flip) is not True:
|
|
if is_empty(flip) is not True:
|
|
flip = int(flip)
|
|
flip = int(flip)
|
|
- dst = cv2.flip(dst, flip)
|
|
|
|
|
|
+ origin = img if dst is None else dst
|
|
|
|
+ dst = cv2.flip(origin, flip)
|
|
|
|
|
|
- if is_empty(rotate) is False:
|
|
|
|
- dst = rot_degree(dst, float(rotate), w=w, h=h)
|
|
|
|
|
|
+ if is_empty(rotate) is False and int(rotate) != 0:
|
|
|
|
+ origin = img if dst is None else dst
|
|
|
|
+ dst, new_w, new_h = rot_degree(origin, float(rotate), w=r_w, h=r_h)
|
|
|
|
+ if new_w is not None:
|
|
|
|
+ r_w = new_w
|
|
|
|
+ r_h = new_h
|
|
|
|
+
|
|
|
|
+ result_path = file_path
|
|
|
|
|
|
if dst is not None:
|
|
if dst is not None:
|
|
cv2.imwrite(out_file, dst)
|
|
cv2.imwrite(out_file, dst)
|
|
- if is_get:
|
|
|
|
- return send_file(out_file, mimetype="image/png")
|
|
|
|
- else:
|
|
|
|
- return jsonify(success_resp({
|
|
|
|
- "fileId": get_file_id(out_file),
|
|
|
|
- "url": file_url(out_file)
|
|
|
|
- }))
|
|
|
|
|
|
+ result_path = out_file
|
|
|
|
+ else:
|
|
|
|
+ dst = img
|
|
|
|
+
|
|
|
|
+ if save:
|
|
|
|
+ result_path = get_output_file_path(file_id)
|
|
|
|
+ cv2.imwrite(result_path, dst)
|
|
|
|
|
|
if is_get:
|
|
if is_get:
|
|
- return send_file(file_path, mimetype="image/png")
|
|
|
|
|
|
+ mimetype = 'application/octet-stream' if download else 'image/png'
|
|
|
|
+ return send_file(result_path, mimetype=mimetype)
|
|
|
|
+
|
|
return jsonify(success_resp({
|
|
return jsonify(success_resp({
|
|
- "fileId": get_file_id(file_path),
|
|
|
|
- "url": file_url(file_path)
|
|
|
|
|
|
+ "fileId": get_file_id(result_path, True),
|
|
|
|
+ "url": file_url(result_path, True),
|
|
|
|
+ "width": r_w,
|
|
|
|
+ "height": r_h,
|
|
}))
|
|
}))
|
|
|
|
|
|
|
|
|
|
def crop(img, rect: str, w, h):
|
|
def crop(img, rect: str, w, h):
|
|
- # 裁剪
|
|
|
|
|
|
+ # 裁剪,
|
|
|
|
+ # 如果没有改变,返回None
|
|
if is_empty(rect):
|
|
if is_empty(rect):
|
|
- return img
|
|
|
|
|
|
+ return None, None, None
|
|
r = rect.split(',')
|
|
r = rect.split(',')
|
|
if len(r) != 4:
|
|
if len(r) != 4:
|
|
- return img
|
|
|
|
|
|
+ return None, None, None
|
|
|
|
|
|
left = int(r[0])
|
|
left = int(r[0])
|
|
top = int(r[1])
|
|
top = int(r[1])
|
|
@@ -156,9 +205,9 @@ def crop(img, rect: str, w, h):
|
|
bottom = h
|
|
bottom = h
|
|
|
|
|
|
if left == 0 and top == 0 and right == w and bottom == h:
|
|
if left == 0 and top == 0 and right == w and bottom == h:
|
|
- return img
|
|
|
|
|
|
+ return None, None, None
|
|
|
|
|
|
- return img[top:bottom, left:right]
|
|
|
|
|
|
+ return img[top:bottom, left:right], right-left, bottom-top
|
|
|
|
|
|
|
|
|
|
def rot_degree(img, degree, w, h):
|
|
def rot_degree(img, degree, w, h):
|
|
@@ -176,4 +225,4 @@ def rot_degree(img, degree, w, h):
|
|
M[0, 2] += offset_x
|
|
M[0, 2] += offset_x
|
|
M[1, 2] += offset_y
|
|
M[1, 2] += offset_y
|
|
dst = cv2.warpAffine(img, M, (new_width, new_height))
|
|
dst = cv2.warpAffine(img, M, (new_width, new_height))
|
|
- return dst
|
|
|
|
|
|
+ return dst, new_width, new_height
|