|
@@ -13,6 +13,9 @@ from .get_mask_by_green import GetMask
|
|
|
from middleware import UnicornException
|
|
from middleware import UnicornException
|
|
|
from logger import logger
|
|
from logger import logger
|
|
|
from custom_plugins.plugins_mode.pic_deal import PictureProcessing
|
|
from custom_plugins.plugins_mode.pic_deal import PictureProcessing
|
|
|
|
|
+from service.remove_bg_ali import Segment
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def time_it(func):
|
|
def time_it(func):
|
|
|
@wraps(func) # 使用wraps来保留原始函数的元数据信息
|
|
@wraps(func) # 使用wraps来保留原始函数的元数据信息
|
|
|
def wrapper(*args, **kwargs):
|
|
def wrapper(*args, **kwargs):
|
|
@@ -33,8 +36,10 @@ class GeneratePic(object):
|
|
|
self.is_test = is_test
|
|
self.is_test = is_test
|
|
|
self.saver = ImageSaver()
|
|
self.saver = ImageSaver()
|
|
|
pass
|
|
pass
|
|
|
|
|
+
|
|
|
@time_it
|
|
@time_it
|
|
|
- def get_mask_and_config_v3(self, im_jpg: Image, im_png: Image, curve_mask: bool,grenerate_main_pic_brightness:int):
|
|
|
|
|
|
|
+ def get_mask_and_config_v3(self, im_jpg: Image, im_png: Image, curve_mask: bool,
|
|
|
|
|
+ grenerate_main_pic_brightness: int):
|
|
|
"""
|
|
"""
|
|
|
步骤:
|
|
步骤:
|
|
|
1、尺寸进行对应缩小
|
|
1、尺寸进行对应缩小
|
|
@@ -46,7 +51,6 @@ class GeneratePic(object):
|
|
|
im_jpg = to_resize(im_jpg, width=600)
|
|
im_jpg = to_resize(im_jpg, width=600)
|
|
|
im_png = to_resize(im_png, width=600)
|
|
im_png = to_resize(im_png, width=600)
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# =========================两个蒙版叠加,删除上半部分的图
|
|
# =========================两个蒙版叠加,删除上半部分的图
|
|
|
# 获取透明图的左右点
|
|
# 获取透明图的左右点
|
|
|
result = get_extremes_from_transparent(im_png)
|
|
result = get_extremes_from_transparent(im_png)
|
|
@@ -109,10 +113,9 @@ class GeneratePic(object):
|
|
|
brightness_value = brightness_check(img_gray=_im_shadow, mask=new_mask)
|
|
brightness_value = brightness_check(img_gray=_im_shadow, mask=new_mask)
|
|
|
|
|
|
|
|
print("循环识别:{},Midtones:{},Highlight:{},brightness_value:{}".format(xunhuan,
|
|
print("循环识别:{},Midtones:{},Highlight:{},brightness_value:{}".format(xunhuan,
|
|
|
- Midtones,
|
|
|
|
|
- Highlight,
|
|
|
|
|
- brightness_value))
|
|
|
|
|
-
|
|
|
|
|
|
|
+ Midtones,
|
|
|
|
|
+ Highlight,
|
|
|
|
|
+ brightness_value))
|
|
|
|
|
|
|
|
if brightness_value >= grenerate_main_pic_brightness:
|
|
if brightness_value >= grenerate_main_pic_brightness:
|
|
|
# //GRENERATE_MAIN_PIC_BRIGHTNESS 亮度校验
|
|
# //GRENERATE_MAIN_PIC_BRIGHTNESS 亮度校验
|
|
@@ -140,6 +143,109 @@ class GeneratePic(object):
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return return_mask, config
|
|
return return_mask, config
|
|
|
|
|
+
|
|
|
|
|
+ @time_it
|
|
|
|
|
+ def get_mask_and_config_v4_online(self, ori_im_jpg: Image, ori_im_png: Image, im_jpg: Image, im_png: Image,
|
|
|
|
|
+ image_mask_opacity, image_mask_grenerate_main_pic_brightness):
|
|
|
|
|
+ print("42------当前计算函数:get_mask_and_config_v4_online")
|
|
|
|
|
+ """
|
|
|
|
|
+ 步骤:
|
|
|
|
|
+ 1、尺寸进行对应缩小
|
|
|
|
|
+ 2、查找并设定鞋底阴影蒙版
|
|
|
|
|
+ 3、自动色阶检查亮度
|
|
|
|
|
+ 4、输出自动色阶参数、以及放大的尺寸蒙版
|
|
|
|
|
+ """
|
|
|
|
|
+ # ===================尺寸进行对应缩小(提升处理速度)
|
|
|
|
|
+ ori_im_jpg = to_resize(ori_im_jpg, width=1800)
|
|
|
|
|
+ ori_im_png = to_resize(ori_im_png, width=1800)
|
|
|
|
|
+ # todo 上传云端返回mask
|
|
|
|
|
+ utrlImage = Segment()
|
|
|
|
|
+ try:
|
|
|
|
|
+ api_url = f"{settings.DOMAIN}/api/ai_image/segment_shadow/shadow_service"
|
|
|
|
|
+ matte_image_url = utrlImage.get_ultra_shadow(ori_im_jpg, ori_im_png, opacity=image_mask_opacity,
|
|
|
|
|
+ bright_target=image_mask_grenerate_main_pic_brightness,
|
|
|
|
|
+ api_url=api_url)
|
|
|
|
|
+ response = requests.get(matte_image_url)
|
|
|
|
|
+ pic = response.content
|
|
|
|
|
+ return_mask = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print("get_mask_and_config_v4_online 异常", e)
|
|
|
|
|
+ return None, None
|
|
|
|
|
+ # todo 上传云端
|
|
|
|
|
+ # ori_im_jpg ori_im_png
|
|
|
|
|
+ # return_mask
|
|
|
|
|
+
|
|
|
|
|
+ # ====================生成新的图片
|
|
|
|
|
+ print("get_mask_and_config_v4_online 生成新的图片", matte_image_url)
|
|
|
|
|
+
|
|
|
|
|
+ # mask 复原尺寸报错一致
|
|
|
|
|
+ return_mask = return_mask.resize(im_png.size)
|
|
|
|
|
+
|
|
|
|
|
+ bg = Image.new(mode="RGB", size=im_png.size, color=(255, 255, 255))
|
|
|
|
|
+ bg.paste(im=im_jpg, mask=return_mask) # 只粘贴有阴影的地方
|
|
|
|
|
+ # bg.show()
|
|
|
|
|
+
|
|
|
|
|
+ # ==================自动色阶处理======================
|
|
|
|
|
+ # 对上述拼接后的图片进行自动色阶处理
|
|
|
|
|
+ _im = cv2.cvtColor(np.asarray(bg), cv2.COLOR_RGB2BGR)
|
|
|
|
|
+ # 背景阴影
|
|
|
|
|
+ im_shadow = cv2.cvtColor(_im, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
+
|
|
|
|
|
+ # print("copy.copy(im_shadow)")
|
|
|
|
|
+ _im_shadow = copy.copy(im_shadow)
|
|
|
|
|
+ Midtones = 0.8
|
|
|
|
|
+ Highlight = 235
|
|
|
|
|
+ k = copy.copy(settings.COLOR_GRADATION_CYCLES)
|
|
|
|
|
+ print("get_mask_and_config_v4_online 开始循环识别")
|
|
|
|
|
+ xunhuan = 0
|
|
|
|
|
+ while k:
|
|
|
|
|
+ xunhuan += 1
|
|
|
|
|
+ k -= 1
|
|
|
|
|
+ Midtones += 0.035
|
|
|
|
|
+ if Midtones > 1.7:
|
|
|
|
|
+ Midtones = 1.7
|
|
|
|
|
+ Highlight -= 3
|
|
|
|
|
+
|
|
|
|
|
+ _im_shadow = levels_adjust(img=im_shadow,
|
|
|
|
|
+ Shadow=0,
|
|
|
|
|
+ Midtones=Midtones,
|
|
|
|
|
+ Highlight=Highlight,
|
|
|
|
|
+ OutShadow=0,
|
|
|
|
|
+ OutHighlight=255, Dim=3)
|
|
|
|
|
+
|
|
|
|
|
+ brightness_value = get_png_brightness(img_gray=_im_shadow, mask=return_mask)
|
|
|
|
|
+
|
|
|
|
|
+ print(
|
|
|
|
|
+ "get_mask_and_config_v4_online----循环识别:{},Midtones:{},Highlight:{},brightness_value:{},阀值:{}".format(
|
|
|
|
|
+ xunhuan,
|
|
|
|
|
+ Midtones,
|
|
|
|
|
+ Highlight,
|
|
|
|
|
+ brightness_value,
|
|
|
|
|
+ settings.GRENERATE_MAIN_PIC_BRIGHTNESS
|
|
|
|
|
+ ))
|
|
|
|
|
+
|
|
|
|
|
+ if brightness_value >= settings.GRENERATE_MAIN_PIC_BRIGHTNESS:
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ im_shadow = cv2_to_pil(_im_shadow)
|
|
|
|
|
+ # ========================================================
|
|
|
|
|
+ # 计算阴影的亮度,用于确保阴影不要太黑
|
|
|
|
|
+
|
|
|
|
|
+ # 1、图片预处理,只保留阴影
|
|
|
|
|
+ only_shadow_img = im_shadow.copy()
|
|
|
|
|
+ only_shadow_img.paste(Image.new(mode="RGBA", size=only_shadow_img.size, color=(255, 255, 255, 255)),
|
|
|
|
|
+ mask=im_png)
|
|
|
|
|
+ # only_shadow_img.show()
|
|
|
|
|
+ average_brightness = calculated_shadow_brightness(only_shadow_img)
|
|
|
|
|
+ print("get_mask_and_config_v4_online average_brightness:", average_brightness)
|
|
|
|
|
+
|
|
|
|
|
+ config = {
|
|
|
|
|
+ "Midtones": Midtones,
|
|
|
|
|
+ "Highlight": Highlight,
|
|
|
|
|
+ "average_brightness": average_brightness,
|
|
|
|
|
+ }
|
|
|
|
|
+ return return_mask, config
|
|
|
|
|
+
|
|
|
@time_it
|
|
@time_it
|
|
|
def get_mask_and_config(self, im_jpg: Image, im_png: Image, curve_mask: bool):
|
|
def get_mask_and_config(self, im_jpg: Image, im_png: Image, curve_mask: bool):
|
|
|
"""
|
|
"""
|
|
@@ -457,104 +563,106 @@ class GeneratePic(object):
|
|
|
time.sleep(3)
|
|
time.sleep(3)
|
|
|
if output_queue is not None:
|
|
if output_queue is not None:
|
|
|
output_queue.put(True)
|
|
output_queue.put(True)
|
|
|
- def paste_img(self,image, top_img, base="nw", value=(0, 0), ):
|
|
|
|
|
- """
|
|
|
|
|
- {
|
|
|
|
|
- "command": "paste_img",
|
|
|
|
|
- "im": 需要粘贴的图片
|
|
|
|
|
- "pos": {"plugins_mode": "relative", # pixel
|
|
|
|
|
- "base": "center", # nw,nc,ne,ec ... 各个方向参考点
|
|
|
|
|
- "value": (100, 100),
|
|
|
|
|
- "percentage": (0.5, 0.5),
|
|
|
|
|
- },
|
|
|
|
|
- "margins": (0, 0, 0, 0), # 上下左右边距
|
|
|
|
|
- }
|
|
|
|
|
- """
|
|
|
|
|
- value = (int(value[0]), int(value[1]))
|
|
|
|
|
- # 处理默认值
|
|
|
|
|
- base = "nw" if not base else base
|
|
|
|
|
- top, down, left, right = 0, 0, 0, 0
|
|
|
|
|
-
|
|
|
|
|
- # 基于右边,上下居中
|
|
|
|
|
- if base == "ec" or base == "ce":
|
|
|
|
|
- p_x = int(image.width - (top_img.width + value[0]))
|
|
|
|
|
- p_y = int((image.height - top_img.height) / 2) + value[1]
|
|
|
|
|
-
|
|
|
|
|
- # 基于顶部,左右居中
|
|
|
|
|
- if base == "nc" or base == "cn":
|
|
|
|
|
- # 顶部对齐
|
|
|
|
|
- deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
- (image.height - top_img.height) / 2
|
|
|
|
|
- )
|
|
|
|
|
- p_x = deviation_x + value[0] + left
|
|
|
|
|
- p_y = value[1]
|
|
|
|
|
-
|
|
|
|
|
- # 基于右上角
|
|
|
|
|
- if base == "en" or base == "ne":
|
|
|
|
|
- p_x = int(image.width - (top_img.width + value[0])) + left
|
|
|
|
|
- p_y = value[1]
|
|
|
|
|
-
|
|
|
|
|
- # 基于左上角
|
|
|
|
|
- if base == "nw" or base == "wn":
|
|
|
|
|
- deviation_x, deviation_y = 0, 0
|
|
|
|
|
- p_x, p_y = value
|
|
|
|
|
-
|
|
|
|
|
- # 基于底部,左右居中
|
|
|
|
|
- if base == "cs" or base == "sc":
|
|
|
|
|
- deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
- (image.height - top_img.height) / 2
|
|
|
|
|
- )
|
|
|
|
|
|
|
|
|
|
- p_y = image.height - (top_img.height + value[1] + down)
|
|
|
|
|
- p_x = deviation_x + value[0] + left
|
|
|
|
|
|
|
+ def paste_img(self, image, top_img, base="nw", value=(0, 0), ):
|
|
|
|
|
+ """
|
|
|
|
|
+ {
|
|
|
|
|
+ "command": "paste_img",
|
|
|
|
|
+ "im": 需要粘贴的图片
|
|
|
|
|
+ "pos": {"plugins_mode": "relative", # pixel
|
|
|
|
|
+ "base": "center", # nw,nc,ne,ec ... 各个方向参考点
|
|
|
|
|
+ "value": (100, 100),
|
|
|
|
|
+ "percentage": (0.5, 0.5),
|
|
|
|
|
+ },
|
|
|
|
|
+ "margins": (0, 0, 0, 0), # 上下左右边距
|
|
|
|
|
+ }
|
|
|
|
|
+ """
|
|
|
|
|
+ value = (int(value[0]), int(value[1]))
|
|
|
|
|
+ # 处理默认值
|
|
|
|
|
+ base = "nw" if not base else base
|
|
|
|
|
+ top, down, left, right = 0, 0, 0, 0
|
|
|
|
|
+
|
|
|
|
|
+ # 基于右边,上下居中
|
|
|
|
|
+ if base == "ec" or base == "ce":
|
|
|
|
|
+ p_x = int(image.width - (top_img.width + value[0]))
|
|
|
|
|
+ p_y = int((image.height - top_img.height) / 2) + value[1]
|
|
|
|
|
+
|
|
|
|
|
+ # 基于顶部,左右居中
|
|
|
|
|
+ if base == "nc" or base == "cn":
|
|
|
|
|
+ # 顶部对齐
|
|
|
|
|
+ deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
+ (image.height - top_img.height) / 2
|
|
|
|
|
+ )
|
|
|
|
|
+ p_x = deviation_x + value[0] + left
|
|
|
|
|
+ p_y = value[1]
|
|
|
|
|
+
|
|
|
|
|
+ # 基于右上角
|
|
|
|
|
+ if base == "en" or base == "ne":
|
|
|
|
|
+ p_x = int(image.width - (top_img.width + value[0])) + left
|
|
|
|
|
+ p_y = value[1]
|
|
|
|
|
+
|
|
|
|
|
+ # 基于左上角
|
|
|
|
|
+ if base == "nw" or base == "wn":
|
|
|
|
|
+ deviation_x, deviation_y = 0, 0
|
|
|
|
|
+ p_x, p_y = value
|
|
|
|
|
+
|
|
|
|
|
+ # 基于底部,左右居中
|
|
|
|
|
+ if base == "cs" or base == "sc":
|
|
|
|
|
+ deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
+ (image.height - top_img.height) / 2
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
- # 上下左右居中
|
|
|
|
|
- if base == "center" or base == "cc":
|
|
|
|
|
- deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
- (image.height - top_img.height) / 2
|
|
|
|
|
- )
|
|
|
|
|
- p_x = deviation_x + value[0] + left
|
|
|
|
|
- p_y = deviation_y + value[1] + top
|
|
|
|
|
|
|
+ p_y = image.height - (top_img.height + value[1] + down)
|
|
|
|
|
+ p_x = deviation_x + value[0] + left
|
|
|
|
|
|
|
|
- # 基于左下角
|
|
|
|
|
- if base == "sw" or base == "ws":
|
|
|
|
|
- # deviation_x, deviation_y = 0, int((img.height - img_1.height))
|
|
|
|
|
- p_x = value[0] + left
|
|
|
|
|
- p_y = image.height - (top_img.height + value[1] + down)
|
|
|
|
|
|
|
+ # 上下左右居中
|
|
|
|
|
+ if base == "center" or base == "cc":
|
|
|
|
|
+ deviation_x, deviation_y = int((image.width - top_img.width) / 2), int(
|
|
|
|
|
+ (image.height - top_img.height) / 2
|
|
|
|
|
+ )
|
|
|
|
|
+ p_x = deviation_x + value[0] + left
|
|
|
|
|
+ p_y = deviation_y + value[1] + top
|
|
|
|
|
|
|
|
- # 基于左边,上下居中
|
|
|
|
|
- if base == "wc" or base == "cw":
|
|
|
|
|
- p_x = value[0] + left
|
|
|
|
|
- p_y = int((image.height - top_img.height) / 2) + value[1] + top
|
|
|
|
|
|
|
+ # 基于左下角
|
|
|
|
|
+ if base == "sw" or base == "ws":
|
|
|
|
|
+ # deviation_x, deviation_y = 0, int((img.height - img_1.height))
|
|
|
|
|
+ p_x = value[0] + left
|
|
|
|
|
+ p_y = image.height - (top_img.height + value[1] + down)
|
|
|
|
|
|
|
|
- # 基于右下角
|
|
|
|
|
- if base == "es" or base == "se":
|
|
|
|
|
- p_x = int(image.width - (top_img.width + value[0])) + left
|
|
|
|
|
- p_y = image.height - (top_img.height + value[1] + down) + top
|
|
|
|
|
|
|
+ # 基于左边,上下居中
|
|
|
|
|
+ if base == "wc" or base == "cw":
|
|
|
|
|
+ p_x = value[0] + left
|
|
|
|
|
+ p_y = int((image.height - top_img.height) / 2) + value[1] + top
|
|
|
|
|
|
|
|
- try:
|
|
|
|
|
- image.paste(top_img, box=(p_x, p_y), mask=top_img)
|
|
|
|
|
- except:
|
|
|
|
|
- image.paste(top_img, box=(p_x, p_y), mask=top_img.convert("RGBA"))
|
|
|
|
|
|
|
+ # 基于右下角
|
|
|
|
|
+ if base == "es" or base == "se":
|
|
|
|
|
+ p_x = int(image.width - (top_img.width + value[0])) + left
|
|
|
|
|
+ p_y = image.height - (top_img.height + value[1] + down) + top
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ image.paste(top_img, box=(p_x, p_y), mask=top_img)
|
|
|
|
|
+ except:
|
|
|
|
|
+ image.paste(top_img, box=(p_x, p_y), mask=top_img.convert("RGBA"))
|
|
|
|
|
+
|
|
|
|
|
+ return image
|
|
|
|
|
|
|
|
- return image
|
|
|
|
|
@time_it
|
|
@time_it
|
|
|
def run(
|
|
def run(
|
|
|
- self,
|
|
|
|
|
- image_path,
|
|
|
|
|
- cut_image_path,
|
|
|
|
|
- out_path,
|
|
|
|
|
- image_deal_mode=0,
|
|
|
|
|
- image_index=99,
|
|
|
|
|
- out_pic_size=1024,
|
|
|
|
|
- is_logo=True,
|
|
|
|
|
- out_process_path_1=None,
|
|
|
|
|
- out_process_path_2=None,
|
|
|
|
|
- resize_mode=None,
|
|
|
|
|
- max_box=None,
|
|
|
|
|
- logo_path="",
|
|
|
|
|
- curve_mask=False,
|
|
|
|
|
- **kwargs,
|
|
|
|
|
|
|
+ self,
|
|
|
|
|
+ image_path,
|
|
|
|
|
+ cut_image_path,
|
|
|
|
|
+ out_path,
|
|
|
|
|
+ image_deal_mode=0,
|
|
|
|
|
+ image_index=99,
|
|
|
|
|
+ out_pic_size=1024,
|
|
|
|
|
+ is_logo=True,
|
|
|
|
|
+ out_process_path_1=None,
|
|
|
|
|
+ out_process_path_2=None,
|
|
|
|
|
+ resize_mode=None,
|
|
|
|
|
+ max_box=None,
|
|
|
|
|
+ logo_path="",
|
|
|
|
|
+ curve_mask=False,
|
|
|
|
|
+ **kwargs,
|
|
|
): # im 为cv对象
|
|
): # im 为cv对象
|
|
|
"""
|
|
"""
|
|
|
image_path:原始图
|
|
image_path:原始图
|
|
@@ -591,23 +699,35 @@ class GeneratePic(object):
|
|
|
with Image.open(cut_image_path) as cut_image:
|
|
with Image.open(cut_image_path) as cut_image:
|
|
|
# 复制图像以便后续操作
|
|
# 复制图像以便后续操作
|
|
|
cut_image = cut_image.copy()
|
|
cut_image = cut_image.copy()
|
|
|
|
|
+ cut_image_basic = cut_image.copy()
|
|
|
cut_image, new_box = get_mini_crop_img(img=cut_image)
|
|
cut_image, new_box = get_mini_crop_img(img=cut_image)
|
|
|
im_shadow = orign_im.crop(new_box) # 切图
|
|
im_shadow = orign_im.crop(new_box) # 切图
|
|
|
new_x, new_y = im_shadow.size
|
|
new_x, new_y = im_shadow.size
|
|
|
|
|
|
|
|
# ================自动色阶处理
|
|
# ================自动色阶处理
|
|
|
_s = time.time()
|
|
_s = time.time()
|
|
|
- image_mask_config = settings.getSysConfigs("basic_configs", "image_mask_config", {"mode":0,"opacity":0.5,"grenerate_main_pic_brightness":254})
|
|
|
|
|
- print("阴影图处理参数===>>>",image_mask_config)
|
|
|
|
|
- image_mask_mode = image_mask_config.get("mode",0)
|
|
|
|
|
- image_mask_opacity = float(image_mask_config.get("opacity",0.5))
|
|
|
|
|
- image_mask_grenerate_main_pic_brightness = int(image_mask_config.get("grenerate_main_pic_brightness",254))
|
|
|
|
|
- if image_mask_mode ==0:
|
|
|
|
|
|
|
+ image_mask_config = settings.getSysConfigs("basic_configs", "image_mask_config",
|
|
|
|
|
+ {"mode": 0, "opacity": 0.5, "grenerate_main_pic_brightness": 254})
|
|
|
|
|
+ print("阴影图处理参数===>>>", image_mask_config)
|
|
|
|
|
+ image_mask_mode = image_mask_config.get("mode", 0)
|
|
|
|
|
+ image_mask_opacity = float(image_mask_config.get("opacity", 0.5))
|
|
|
|
|
+ image_mask_grenerate_main_pic_brightness = int(image_mask_config.get("grenerate_main_pic_brightness", 254))
|
|
|
|
|
+ if image_mask_mode == 0:
|
|
|
shadow_mask, config = self.get_mask_and_config(
|
|
shadow_mask, config = self.get_mask_and_config(
|
|
|
im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask
|
|
im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask
|
|
|
)
|
|
)
|
|
|
|
|
+ elif image_mask_mode == 1:
|
|
|
|
|
+ shadow_mask, config = self.get_mask_and_config_v3(im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask,
|
|
|
|
|
+ grenerate_main_pic_brightness=image_mask_grenerate_main_pic_brightness)
|
|
|
|
|
+ elif image_mask_mode == 2:
|
|
|
|
|
+ shadow_mask, config = self.get_mask_and_config_v4_online(ori_im_jpg=orign_im, ori_im_png=cut_image_basic,
|
|
|
|
|
+ im_jpg=im_shadow,
|
|
|
|
|
+ im_png=cut_image,
|
|
|
|
|
+ image_mask_opacity=image_mask_opacity,
|
|
|
|
|
+ image_mask_grenerate_main_pic_brightness=image_mask_grenerate_main_pic_brightness)
|
|
|
else:
|
|
else:
|
|
|
- shadow_mask, config = self.get_mask_and_config_v3(im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask,grenerate_main_pic_brightness=image_mask_grenerate_main_pic_brightness)
|
|
|
|
|
|
|
+ shadow_mask, config = self.get_mask_and_config_v3(im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask,
|
|
|
|
|
+ grenerate_main_pic_brightness=image_mask_grenerate_main_pic_brightness)
|
|
|
print("242 need_time_2:{}".format(time.time() - _s))
|
|
print("242 need_time_2:{}".format(time.time() - _s))
|
|
|
|
|
|
|
|
shadow_mask = shadow_mask.resize(im_shadow.size)
|
|
shadow_mask = shadow_mask.resize(im_shadow.size)
|
|
@@ -635,7 +755,7 @@ class GeneratePic(object):
|
|
|
|
|
|
|
|
# ================处理阴影的亮度==================
|
|
# ================处理阴影的亮度==================
|
|
|
average_brightness = config["average_brightness"]
|
|
average_brightness = config["average_brightness"]
|
|
|
- if image_mask_mode ==0:
|
|
|
|
|
|
|
+ if image_mask_mode == 0:
|
|
|
if config["average_brightness"] < 180:
|
|
if config["average_brightness"] < 180:
|
|
|
# 调整阴影亮度
|
|
# 调整阴影亮度
|
|
|
backdrop_prepped = np.asfarray(
|
|
backdrop_prepped = np.asfarray(
|
|
@@ -656,8 +776,8 @@ class GeneratePic(object):
|
|
|
# im_shadow.show()
|
|
# im_shadow.show()
|
|
|
else:
|
|
else:
|
|
|
backdrop_prepped = np.asfarray(
|
|
backdrop_prepped = np.asfarray(
|
|
|
- Image.new(mode="RGBA", size=im_shadow.size, color=(255, 255, 255, 255))
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ Image.new(mode="RGBA", size=im_shadow.size, color=(255, 255, 255, 255))
|
|
|
|
|
+ )
|
|
|
im_shadow = im_shadow.convert("RGBA")
|
|
im_shadow = im_shadow.convert("RGBA")
|
|
|
source_prepped = np.asfarray(im_shadow)
|
|
source_prepped = np.asfarray(im_shadow)
|
|
|
opacity_params = int(image_mask_opacity * 100)
|
|
opacity_params = int(image_mask_opacity * 100)
|
|
@@ -679,7 +799,7 @@ class GeneratePic(object):
|
|
|
out_image_1 = out_image_1.transpose(Image.FLIP_LEFT_RIGHT)
|
|
out_image_1 = out_image_1.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
|
|
|
|
|
self.saver.save_image(
|
|
self.saver.save_image(
|
|
|
- image=out_image_1, file_path=out_process_path_1, quality=100,dpi=(350, 350), _format="PNG"
|
|
|
|
|
|
|
+ image=out_image_1, file_path=out_process_path_1, quality=100, dpi=(350, 350), _format="PNG"
|
|
|
)
|
|
)
|
|
|
# save_image_by_thread(image=out_image_1, out_path=out_process_path_1)
|
|
# save_image_by_thread(image=out_image_1, out_path=out_process_path_1)
|
|
|
# out_image_1.save(out_process_path_1)
|
|
# out_image_1.save(out_process_path_1)
|
|
@@ -691,7 +811,7 @@ class GeneratePic(object):
|
|
|
out_image_2 = out_image_2.transpose(Image.FLIP_LEFT_RIGHT)
|
|
out_image_2 = out_image_2.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
|
|
|
|
|
self.saver.save_image(
|
|
self.saver.save_image(
|
|
|
- image=out_image_2, file_path=out_process_path_2, quality=100,dpi=(350, 350), _format="PNG"
|
|
|
|
|
|
|
+ image=out_image_2, file_path=out_process_path_2, quality=100, dpi=(350, 350), _format="PNG"
|
|
|
)
|
|
)
|
|
|
# save_image_by_thread(image=out_image_2, out_path=out_process_path_2, save_mode="png")
|
|
# save_image_by_thread(image=out_image_2, out_path=out_process_path_2, save_mode="png")
|
|
|
# out_image_2.save(out_process_path_2)
|
|
# out_image_2.save(out_process_path_2)
|
|
@@ -717,7 +837,8 @@ class GeneratePic(object):
|
|
|
offset_x, offset_y = _x - (cut_image.width - _w) / 2, _y - (cut_image.height - _h) / 2,
|
|
offset_x, offset_y = _x - (cut_image.width - _w) / 2, _y - (cut_image.height - _h) / 2,
|
|
|
# print("中心偏移量:", offset_x, offset_y)
|
|
# print("中心偏移量:", offset_x, offset_y)
|
|
|
# 透明底最小矩形
|
|
# 透明底最小矩形
|
|
|
- scale_rate = self.get_scale(base_by_box=(bg_size[0] - image_margin * 2, bg_size[1] - image_margin * 2), image_size=(_w, _h))
|
|
|
|
|
|
|
+ scale_rate = self.get_scale(base_by_box=(bg_size[0] - image_margin * 2, bg_size[1] - image_margin * 2),
|
|
|
|
|
+ image_size=(_w, _h))
|
|
|
# 计算缩放比例,以及顶点相对位置
|
|
# 计算缩放比例,以及顶点相对位置
|
|
|
# print("缩放比例:", scale_rate)
|
|
# print("缩放比例:", scale_rate)
|
|
|
# 偏移量
|
|
# 偏移量
|
|
@@ -751,7 +872,6 @@ class GeneratePic(object):
|
|
|
im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
|
cut_image = to_resize(_im=cut_image, high=1400)
|
|
cut_image = to_resize(_im=cut_image, high=1400)
|
|
|
|
|
|
|
|
-
|
|
|
|
|
# 创建底层背景
|
|
# 创建底层背景
|
|
|
# 用户可设置的颜色值参数
|
|
# 用户可设置的颜色值参数
|
|
|
# image_bg = Image.new("RGB", bg_size, rgb_color)
|
|
# image_bg = Image.new("RGB", bg_size, rgb_color)
|
|
@@ -759,10 +879,10 @@ class GeneratePic(object):
|
|
|
# image_bg = self.paste_img(image=image_bg, top_img=cut_image, base="cc", value=(_offset_x * -1, _offset_y * -1))
|
|
# image_bg = self.paste_img(image=image_bg, top_img=cut_image, base="cc", value=(_offset_x * -1, _offset_y * -1))
|
|
|
image_bg = PictureProcessing("RGB", bg_size, rgb_color)
|
|
image_bg = PictureProcessing("RGB", bg_size, rgb_color)
|
|
|
image_bg = image_bg.to_overlay_pic_advance(mode="pixel",
|
|
image_bg = image_bg.to_overlay_pic_advance(mode="pixel",
|
|
|
- top_img=PictureProcessing(im=im_shadow),
|
|
|
|
|
- base="cc",
|
|
|
|
|
- value=(_offset_x * -1, _offset_y * -1),
|
|
|
|
|
- top_png_img=PictureProcessing(im=cut_image),)
|
|
|
|
|
|
|
+ top_img=PictureProcessing(im=im_shadow),
|
|
|
|
|
+ base="cc",
|
|
|
|
|
+ value=(_offset_x * -1, _offset_y * -1),
|
|
|
|
|
+ top_png_img=PictureProcessing(im=cut_image), )
|
|
|
image_bg = image_bg.im
|
|
image_bg = image_bg.im
|
|
|
image_bg_x, image_bg_y = image_bg.size
|
|
image_bg_x, image_bg_y = image_bg.size
|
|
|
image_x, image_y = im_shadow.size
|
|
image_x, image_y = im_shadow.size
|
|
@@ -809,7 +929,7 @@ class GeneratePic(object):
|
|
|
if dot_index != -1:
|
|
if dot_index != -1:
|
|
|
# 拆分文件路径和后缀
|
|
# 拆分文件路径和后缀
|
|
|
file_without_suffix = out_path[:dot_index]
|
|
file_without_suffix = out_path[:dot_index]
|
|
|
- suffix = out_path[dot_index + 1 :]
|
|
|
|
|
|
|
+ suffix = out_path[dot_index + 1:]
|
|
|
else:
|
|
else:
|
|
|
file_without_suffix = out_path
|
|
file_without_suffix = out_path
|
|
|
suffix = ""
|
|
suffix = ""
|
|
@@ -818,8 +938,8 @@ class GeneratePic(object):
|
|
|
image_size_str = str(imageSize)
|
|
image_size_str = str(imageSize)
|
|
|
new_file_path = f"{file_without_suffix}_{image_size_str}.{suffix}"
|
|
new_file_path = f"{file_without_suffix}_{image_size_str}.{suffix}"
|
|
|
image_bg = image_bg.resize(
|
|
image_bg = image_bg.resize(
|
|
|
- (image_size_int, image_size_int), resample=settings.RESIZE_IMAGE_MODE
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ (image_size_int, image_size_int), resample=settings.RESIZE_IMAGE_MODE
|
|
|
|
|
+ )
|
|
|
if image_size_int < 3000:
|
|
if image_size_int < 3000:
|
|
|
if out_pci_mode == ".jpg":
|
|
if out_pci_mode == ".jpg":
|
|
|
self.saver.save_image(
|
|
self.saver.save_image(
|
|
@@ -872,11 +992,12 @@ class GeneratePic(object):
|
|
|
if output_queue is not None:
|
|
if output_queue is not None:
|
|
|
output_queue.put(True)
|
|
output_queue.put(True)
|
|
|
return True
|
|
return True
|
|
|
- def get_scale(self,base_by_box, image_size):
|
|
|
|
|
|
|
+
|
|
|
|
|
+ def get_scale(self, base_by_box, image_size):
|
|
|
box_width, box_height = int(base_by_box[0]), int(base_by_box[1])
|
|
box_width, box_height = int(base_by_box[0]), int(base_by_box[1])
|
|
|
width, height = image_size[0], image_size[1]
|
|
width, height = image_size[0], image_size[1]
|
|
|
if box_width / box_height < width / height:
|
|
if box_width / box_height < width / height:
|
|
|
scale = box_width / width
|
|
scale = box_width / width
|
|
|
else:
|
|
else:
|
|
|
scale = box_height / height
|
|
scale = box_height / height
|
|
|
- return scale
|
|
|
|
|
|
|
+ return scale
|