|
|
@@ -13,6 +13,9 @@ from .get_mask_by_green import GetMask
|
|
|
from middleware import UnicornException
|
|
|
from logger import logger
|
|
|
from custom_plugins.plugins_mode.pic_deal import PictureProcessing
|
|
|
+from service.remove_bg_ali import Segment
|
|
|
+
|
|
|
+
|
|
|
def time_it(func):
|
|
|
@wraps(func) # 使用wraps来保留原始函数的元数据信息
|
|
|
def wrapper(*args, **kwargs):
|
|
|
@@ -33,8 +36,10 @@ 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):
|
|
|
+ def get_mask_and_config_v3(self, im_jpg: Image, im_png: Image, curve_mask: bool,
|
|
|
+ grenerate_main_pic_brightness: int):
|
|
|
"""
|
|
|
步骤:
|
|
|
1、尺寸进行对应缩小
|
|
|
@@ -46,7 +51,6 @@ class GeneratePic(object):
|
|
|
im_jpg = to_resize(im_jpg, width=600)
|
|
|
im_png = to_resize(im_png, width=600)
|
|
|
|
|
|
-
|
|
|
# =========================两个蒙版叠加,删除上半部分的图
|
|
|
# 获取透明图的左右点
|
|
|
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)
|
|
|
|
|
|
print("循环识别:{},Midtones:{},Highlight:{},brightness_value:{}".format(xunhuan,
|
|
|
- Midtones,
|
|
|
- Highlight,
|
|
|
- brightness_value))
|
|
|
-
|
|
|
+ Midtones,
|
|
|
+ Highlight,
|
|
|
+ brightness_value))
|
|
|
|
|
|
if brightness_value >= grenerate_main_pic_brightness:
|
|
|
# //GRENERATE_MAIN_PIC_BRIGHTNESS 亮度校验
|
|
|
@@ -140,6 +143,148 @@ class GeneratePic(object):
|
|
|
}
|
|
|
|
|
|
return return_mask, config
|
|
|
+
|
|
|
+ @time_it
|
|
|
+ def fast_remove_black_dots(self, mask_img, max_area=50):
|
|
|
+ """
|
|
|
+ 极速去除黑白蒙版中面积 <= max_area 的黑色噪点
|
|
|
+ """
|
|
|
+ # 1. 确保是纯黑白二值图 (0 和 255)
|
|
|
+ # 如果原图有灰度,先做二值化,否则连通域计算会出错
|
|
|
+ if mask_img.mode != 'L':
|
|
|
+ mask_img = mask_img.convert('L')
|
|
|
+ img_array = np.array(mask_img)
|
|
|
+ _, binary = cv2.threshold(img_array, 127, 255, cv2.THRESH_BINARY)
|
|
|
+
|
|
|
+ # 2. 连通域分析 (C++底层,极快)
|
|
|
+ # 注意:OpenCV 默认白色(255)是前景,黑色(0)是背景
|
|
|
+ # 我们要找的是“黑色的噪点”,所以先取反
|
|
|
+ inv_binary = cv2.bitwise_not(binary)
|
|
|
+ num_labels, labels, stats, _ = cv2.connectedComponentsWithStats(inv_binary, connectivity=8)
|
|
|
+
|
|
|
+ # 3. 提取需要去除的黑色区域
|
|
|
+ # stats 包含每个连通域的面积 (cv2.CC_STAT_AREA)
|
|
|
+ # 第 0 个标签是背景(原图的大面积白色),从 1 开始是黑色噪点
|
|
|
+ areas = stats[1:, cv2.CC_STAT_AREA]
|
|
|
+ small_dots_indices = np.where(areas <= max_area)[0] + 1 # 索引要加1,对应回 labels
|
|
|
+
|
|
|
+ # 4. 批量修改原图(向量化操作,无需 for 循环)
|
|
|
+ mask_to_remove = np.isin(labels, small_dots_indices)
|
|
|
+ binary[mask_to_remove] = 255 # 将小噪点涂白
|
|
|
+ return Image.fromarray(binary, mode='L')
|
|
|
+
|
|
|
+ @time_it
|
|
|
+ def get_mask_and_config_v4_online(self, ori_im_jpg: Image, ori_im_png: Image, im_jpg: Image, im_png: Image):
|
|
|
+ print("179------当前计算函数:get_mask_and_config_v4_online")
|
|
|
+ """
|
|
|
+ 步骤:
|
|
|
+ 1、尺寸进行对应缩小
|
|
|
+ 2、查找并设定鞋底阴影蒙版
|
|
|
+ 3、自动色阶检查亮度
|
|
|
+ 4、输出自动色阶参数、以及放大的尺寸蒙版
|
|
|
+ """
|
|
|
+ # ===================尺寸进行对应缩小(提升处理速度)
|
|
|
+ ori_im_jpg = to_resize(ori_im_jpg, width=1200)
|
|
|
+ ori_im_png = to_resize(ori_im_png, width=1200)
|
|
|
+
|
|
|
+ im_jpg = to_resize(im_jpg, width=600)
|
|
|
+ im_png = to_resize(im_png, width=600)
|
|
|
+ segment = Segment()
|
|
|
+ api_url = f"{settings.DOMAIN}/api/ai_image/segment_shadow/platform_shadow"
|
|
|
+ bg_mask_image_url = segment.get_platform_shadow(ori_im_jpg, api_url=api_url)
|
|
|
+ if bg_mask_image_url:
|
|
|
+ response = requests.get(bg_mask_image_url)
|
|
|
+ pic = response.content
|
|
|
+ bg_mask = Image.open(BytesIO(pic)) # 阿里返回的抠图结果 已转PIL对象
|
|
|
+ bg_mask = bg_mask.convert("L")
|
|
|
+ bg_mask = ImageChops.invert(bg_mask)
|
|
|
+ _, new_box = get_mini_crop_img(img=ori_im_png)
|
|
|
+ bg_mask = bg_mask.crop(new_box) # 切图
|
|
|
+ bg_mask = bg_mask.resize(im_png.size)
|
|
|
+ bg_mask = self.fast_remove_black_dots(bg_mask, max_area=50)
|
|
|
+ # bg_mask = expand_or_shrink_mask(pil_image=bg_mask, expansion_radius=6, blur_radius=0)
|
|
|
+ else:
|
|
|
+ bg_mask = False
|
|
|
+ bg_mask = Image.new("RGB", im_png.size, (255, 255, 255))
|
|
|
+ bg_mask = bg_mask.convert("L")
|
|
|
+
|
|
|
+ # 透明图转mask 将原图扩边一些,并填充白色
|
|
|
+ shoe_png_mask = transparent_to_mask_pil(im_png, is_invert=False)
|
|
|
+ shoe_png_mask = expand_or_shrink_mask(pil_image=shoe_png_mask, expansion_radius=40, blur_radius=0)
|
|
|
+ shoe_png_mask = ImageChops.invert(shoe_png_mask)
|
|
|
+ # 两个mask 取交集
|
|
|
+ if bg_mask is not False:
|
|
|
+ # new_mask 背景+鞋子+鞋子阴影的mask
|
|
|
+ new_mask = mask_intersection(shoe_png_mask, bg_mask)
|
|
|
+ else:
|
|
|
+ new_mask = shoe_png_mask
|
|
|
+
|
|
|
+ # 黑色表示鞋子+背景
|
|
|
+ # new_mask.show()
|
|
|
+
|
|
|
+ # ====================生成图片(一张图减去背景、减去阴影、减去鞋子,即只有底盘的图片,其他区域为白色)
|
|
|
+ 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)
|
|
|
+
|
|
|
+ _im_shadow = copy.copy(im_shadow)
|
|
|
+
|
|
|
+ Midtones = 0.8
|
|
|
+ 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 = get_png_brightness(img_gray=_im_shadow, mask=new_mask)
|
|
|
+
|
|
|
+ print("128----循环识别:{},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("average_brightness:", average_brightness)
|
|
|
+
|
|
|
+ config = {
|
|
|
+ "Midtones": Midtones,
|
|
|
+ "Highlight": Highlight,
|
|
|
+ "average_brightness": average_brightness,
|
|
|
+ }
|
|
|
+ return bg_mask, config
|
|
|
+
|
|
|
@time_it
|
|
|
def get_mask_and_config(self, im_jpg: Image, im_png: Image, curve_mask: bool):
|
|
|
"""
|
|
|
@@ -457,104 +602,106 @@ class GeneratePic(object):
|
|
|
time.sleep(3)
|
|
|
if output_queue is not None:
|
|
|
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
|
|
|
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对象
|
|
|
"""
|
|
|
image_path:原始图
|
|
|
@@ -583,31 +730,37 @@ class GeneratePic(object):
|
|
|
rgb_color = settings.hex_to_rgb(color_800image)
|
|
|
# ==========先进行剪切原图
|
|
|
_s = time.time()
|
|
|
- with Image.open(image_path) as orign_im:
|
|
|
- # 复制图像以便后续操作
|
|
|
- orign_im = orign_im.copy()
|
|
|
+ orign_im = Image.open(image_path)
|
|
|
print("242 need_time_1:{}".format(time.time() - _s))
|
|
|
orign_x, orign_y = orign_im.size
|
|
|
- with Image.open(cut_image_path) as cut_image:
|
|
|
- # 复制图像以便后续操作
|
|
|
- cut_image = cut_image.copy()
|
|
|
- cut_image, new_box = get_mini_crop_img(img=cut_image)
|
|
|
+ orign_im_cut = Image.open(cut_image_path) # 原始图的已扣图
|
|
|
+ cut_image, new_box = get_mini_crop_img(img=orign_im_cut)
|
|
|
im_shadow = orign_im.crop(new_box) # 切图
|
|
|
new_x, new_y = im_shadow.size
|
|
|
|
|
|
# ================自动色阶处理
|
|
|
_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(
|
|
|
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=orign_im_cut,
|
|
|
+ im_jpg=im_shadow,
|
|
|
+ im_png=cut_image)
|
|
|
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))
|
|
|
|
|
|
shadow_mask = shadow_mask.resize(im_shadow.size)
|
|
|
@@ -635,7 +788,7 @@ class GeneratePic(object):
|
|
|
|
|
|
# ================处理阴影的亮度==================
|
|
|
average_brightness = config["average_brightness"]
|
|
|
- if image_mask_mode ==0:
|
|
|
+ if image_mask_mode == 0:
|
|
|
if config["average_brightness"] < 180:
|
|
|
# 调整阴影亮度
|
|
|
backdrop_prepped = np.asfarray(
|
|
|
@@ -656,8 +809,8 @@ class GeneratePic(object):
|
|
|
# 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)
|
|
|
opacity_params = int(image_mask_opacity * 100)
|
|
|
@@ -679,7 +832,7 @@ class GeneratePic(object):
|
|
|
out_image_1 = out_image_1.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
|
|
|
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)
|
|
|
# out_image_1.save(out_process_path_1)
|
|
|
@@ -691,7 +844,7 @@ class GeneratePic(object):
|
|
|
out_image_2 = out_image_2.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
|
|
|
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")
|
|
|
# out_image_2.save(out_process_path_2)
|
|
|
@@ -717,7 +870,8 @@ class GeneratePic(object):
|
|
|
offset_x, offset_y = _x - (cut_image.width - _w) / 2, _y - (cut_image.height - _h) / 2,
|
|
|
# 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)
|
|
|
# 偏移量
|
|
|
@@ -751,7 +905,6 @@ class GeneratePic(object):
|
|
|
im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
|
cut_image = to_resize(_im=cut_image, high=1400)
|
|
|
|
|
|
-
|
|
|
# 创建底层背景
|
|
|
# 用户可设置的颜色值参数
|
|
|
# image_bg = Image.new("RGB", bg_size, rgb_color)
|
|
|
@@ -759,10 +912,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 = PictureProcessing("RGB", bg_size, rgb_color)
|
|
|
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_x, image_bg_y = image_bg.size
|
|
|
image_x, image_y = im_shadow.size
|
|
|
@@ -809,7 +962,7 @@ class GeneratePic(object):
|
|
|
if dot_index != -1:
|
|
|
# 拆分文件路径和后缀
|
|
|
file_without_suffix = out_path[:dot_index]
|
|
|
- suffix = out_path[dot_index + 1 :]
|
|
|
+ suffix = out_path[dot_index + 1:]
|
|
|
else:
|
|
|
file_without_suffix = out_path
|
|
|
suffix = ""
|
|
|
@@ -818,8 +971,8 @@ class GeneratePic(object):
|
|
|
image_size_str = str(imageSize)
|
|
|
new_file_path = f"{file_without_suffix}_{image_size_str}.{suffix}"
|
|
|
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 out_pci_mode == ".jpg":
|
|
|
self.saver.save_image(
|
|
|
@@ -872,11 +1025,12 @@ class GeneratePic(object):
|
|
|
if output_queue is not None:
|
|
|
output_queue.put(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])
|
|
|
width, height = image_size[0], image_size[1]
|
|
|
if box_width / box_height < width / height:
|
|
|
scale = box_width / width
|
|
|
else:
|
|
|
scale = box_height / height
|
|
|
- return scale
|
|
|
+ return scale
|