|
|
@@ -33,7 +33,113 @@ class GeneratePic(object):
|
|
|
self.is_test = is_test
|
|
|
self.saver = ImageSaver()
|
|
|
pass
|
|
|
+ @time_it
|
|
|
+ def get_mask_and_config_v3(self, im_jpg: Image, im_png: Image, curve_mask: bool,grenerate_main_pic_brightness:int):
|
|
|
+ """
|
|
|
+ 步骤:
|
|
|
+ 1、尺寸进行对应缩小
|
|
|
+ 2、查找并设定鞋底阴影蒙版
|
|
|
+ 3、自动色阶检查亮度
|
|
|
+ 4、输出自动色阶参数、以及放大的尺寸蒙版
|
|
|
+ """
|
|
|
+ # ===================尺寸进行对应缩小(提升处理速度)
|
|
|
+ im_jpg = to_resize(im_jpg, width=600)
|
|
|
+ im_png = to_resize(im_png, width=600)
|
|
|
+
|
|
|
+
|
|
|
+ # =========================两个蒙版叠加,删除上半部分的图
|
|
|
+ # 获取透明图的左右点
|
|
|
+ result = get_extremes_from_transparent(im_png)
|
|
|
+ # 创建多边形mask(并进行左右偏移)
|
|
|
+ left_point = (result["leftmost"][0], result["leftmost"][1] - 50)
|
|
|
+ right_point = (result["rightmost"][0], result["rightmost"][1] - 50)
|
|
|
+ mask_other_2 = create_polygon_mask_from_points(img=im_png, left_point=left_point, right_point=right_point)
|
|
|
+
|
|
|
+ # 透明图转mask 将原图扩边一些,并填充白色
|
|
|
+ mask_other_1 = transparent_to_mask_pil(im_png, is_invert=False)
|
|
|
+ mask_other_1 = expand_or_shrink_mask(pil_image=mask_other_1, expansion_radius=40, blur_radius=0)
|
|
|
+ new_image_1 = Image.new("RGBA", im_png.size, (255, 255, 255, 0))
|
|
|
+ im_grey_jpg = im_jpg.convert("L").convert("RGB")
|
|
|
+ inverted_mask_other_1 = ImageChops.invert(mask_other_1)
|
|
|
+
|
|
|
+ # 两个mask 取交集
|
|
|
+ mask_other_2 = mask_other_2.convert("L")
|
|
|
+ # 返回的蒙版区域
|
|
|
+ return_mask = mask_other_2
|
|
|
+
|
|
|
+ new_mask = mask_intersection(inverted_mask_other_1, mask_other_2)
|
|
|
+ # new_mask.show()
|
|
|
+ # return_mask.show()
|
|
|
+ # TODO 待移除
|
|
|
+
|
|
|
+ # ====================生成新的图片
|
|
|
+ print("84 生成新的图片")
|
|
|
+ bg = Image.new(mode="RGB", size=im_png.size, color=(255, 255, 255))
|
|
|
+ bg.paste(im=im_jpg, mask=new_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.7
|
|
|
+ Highlight = 235
|
|
|
+ k = copy.copy(settings.COLOR_GRADATION_CYCLES)
|
|
|
+ print("开始循环识别")
|
|
|
+ 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 = brightness_check(img_gray=_im_shadow, mask=new_mask)
|
|
|
|
|
|
+ print("循环识别:{},Midtones:{},Highlight:{},brightness_value:{}".format(xunhuan,
|
|
|
+ Midtones,
|
|
|
+ Highlight,
|
|
|
+ brightness_value))
|
|
|
+
|
|
|
+
|
|
|
+ if brightness_value >= grenerate_main_pic_brightness:
|
|
|
+ # //GRENERATE_MAIN_PIC_BRIGHTNESS 亮度校验
|
|
|
+ break
|
|
|
+
|
|
|
+ im_shadow = cv2_to_pil(_im_shadow)
|
|
|
+ # if self.is_test:
|
|
|
+ # im_shadow.show()
|
|
|
+
|
|
|
+ # ========================================================
|
|
|
+ # 计算阴影的亮度,用于确保阴影不要太黑
|
|
|
+
|
|
|
+ # 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("average_brightness:", average_brightness)
|
|
|
+
|
|
|
+ config = {
|
|
|
+ "Midtones": Midtones,
|
|
|
+ "Highlight": Highlight,
|
|
|
+ "average_brightness": average_brightness,
|
|
|
+ }
|
|
|
+
|
|
|
+ return return_mask, config
|
|
|
@time_it
|
|
|
def get_mask_and_config(self, im_jpg: Image, im_png: Image, curve_mask: bool):
|
|
|
"""
|
|
|
@@ -491,9 +597,17 @@ class GeneratePic(object):
|
|
|
|
|
|
# ================自动色阶处理
|
|
|
_s = time.time()
|
|
|
- shadow_mask, config = self.get_mask_and_config(
|
|
|
- im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask
|
|
|
- )
|
|
|
+ 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(
|
|
|
+ im_jpg=im_shadow, im_png=cut_image, curve_mask=curve_mask
|
|
|
+ )
|
|
|
+ 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)
|
|
|
print("242 need_time_2:{}".format(time.time() - _s))
|
|
|
|
|
|
shadow_mask = shadow_mask.resize(im_shadow.size)
|
|
|
@@ -521,25 +635,37 @@ class GeneratePic(object):
|
|
|
|
|
|
# ================处理阴影的亮度==================
|
|
|
average_brightness = config["average_brightness"]
|
|
|
- if config["average_brightness"] < 180:
|
|
|
- # 调整阴影亮度
|
|
|
+ if image_mask_mode ==0:
|
|
|
+ if config["average_brightness"] < 180:
|
|
|
+ # 调整阴影亮度
|
|
|
+ backdrop_prepped = np.asfarray(
|
|
|
+ Image.new(mode="RGBA", size=im_shadow.size, color=(255, 255, 255, 255))
|
|
|
+ )
|
|
|
+ im_shadow = im_shadow.convert("RGBA")
|
|
|
+ source_prepped = np.asfarray(im_shadow)
|
|
|
+ # im_shadow.show()
|
|
|
+
|
|
|
+ opacity = (average_brightness - 30) / 160
|
|
|
+ opacity = max(0.5, min(opacity, 1))
|
|
|
+
|
|
|
+ print("阴影透明度:{}%".format(int(opacity * 100)))
|
|
|
+ blended_np = multiply(
|
|
|
+ backdrop_prepped, source_prepped, opacity=int(opacity * 100) / 100
|
|
|
+ )
|
|
|
+ im_shadow = Image.fromarray(np.uint8(blended_np)).convert("RGB")
|
|
|
+ # im_shadow.show()
|
|
|
+ else:
|
|
|
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")
|
|
|
source_prepped = np.asfarray(im_shadow)
|
|
|
- # im_shadow.show()
|
|
|
-
|
|
|
- opacity = (average_brightness - 30) / 160
|
|
|
- opacity = max(0.5, min(opacity, 1))
|
|
|
-
|
|
|
- print("阴影透明度:{}%".format(int(opacity * 100)))
|
|
|
+ opacity_params = int(image_mask_opacity * 100)
|
|
|
+ print("阴影透明度:{}%".format(opacity_params))
|
|
|
blended_np = multiply(
|
|
|
- backdrop_prepped, source_prepped, opacity=int(opacity * 100) / 100
|
|
|
+ backdrop_prepped, source_prepped, opacity=opacity_params / 100
|
|
|
)
|
|
|
im_shadow = Image.fromarray(np.uint8(blended_np)).convert("RGB")
|
|
|
- # im_shadow.show()
|
|
|
-
|
|
|
# 把原图粘贴回去,避免色差
|
|
|
im_shadow.paste(cut_image, (0, 0), mask=cut_image)
|
|
|
# _new_im_shadow.show()
|