|
|
@@ -350,7 +350,87 @@ 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
|
|
|
+
|
|
|
+ # 上下左右居中
|
|
|
+ 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 == "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 == "wc" or base == "cw":
|
|
|
+ p_x = value[0] + left
|
|
|
+ p_y = int((image.height - top_img.height) / 2) + value[1] + top
|
|
|
|
|
|
+ # 基于右下角
|
|
|
+ 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
|
|
|
@time_it
|
|
|
def run(
|
|
|
self,
|
|
|
@@ -386,7 +466,10 @@ class GeneratePic(object):
|
|
|
output_queue = kwargs["output_queue"]
|
|
|
else:
|
|
|
output_queue = None
|
|
|
-
|
|
|
+ # image_deal_mode = 0#不翻转图像
|
|
|
+ padding_800image = settings.getSysConfigs(
|
|
|
+ "basic_configs", "padding_800image", 100
|
|
|
+ )
|
|
|
# ==========先进行剪切原图
|
|
|
_s = time.time()
|
|
|
orign_im = Image.open(image_path) # 原始图
|
|
|
@@ -482,46 +565,70 @@ class GeneratePic(object):
|
|
|
# 不生成主图时直接退出
|
|
|
if not out_path:
|
|
|
return True
|
|
|
-
|
|
|
- # im_shadow.show()
|
|
|
- # =====================主图物体的缩放依据大小
|
|
|
- if max_box:
|
|
|
- im_shadow = to_resize(_im=im_shadow, width=max_box[0], high=max_box[1])
|
|
|
- cut_image = to_resize(_im=cut_image, width=max_box[0], high=max_box[1])
|
|
|
- else:
|
|
|
- if resize_mode is None:
|
|
|
- im_shadow = to_resize(_im=im_shadow, width=1400, high=1400)
|
|
|
- cut_image = to_resize(_im=cut_image, width=1400, high=1400)
|
|
|
-
|
|
|
- elif resize_mode == 1:
|
|
|
- im_shadow = to_resize(_im=im_shadow, width=1400, high=1400)
|
|
|
- cut_image = to_resize(_im=cut_image, width=1400, high=1400)
|
|
|
-
|
|
|
- elif resize_mode == 2:
|
|
|
- # todo 兼容长筒靴等,将图片大小限制在一个指定的box内
|
|
|
- im_shadow = to_resize(_im=im_shadow, width=650)
|
|
|
- cut_image = to_resize(_im=cut_image, width=650)
|
|
|
- # 再次检查需要约束缩小到一定高度,适应长筒靴
|
|
|
- _im_x, _im_y = cut_image.size
|
|
|
- if _im_y > 1400:
|
|
|
- im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
|
- cut_image = to_resize(_im=cut_image, high=1400)
|
|
|
-
|
|
|
- # if im_shadow.height <= im_shadow.width * 1.2:
|
|
|
- # im_shadow = to_resize(_im=im_shadow, width=650)
|
|
|
- # cut_image = to_resize(_im=cut_image, width=650)
|
|
|
- # else:
|
|
|
- # im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
|
- # cut_image = to_resize(_im=cut_image, high=1400)
|
|
|
-
|
|
|
if image_deal_mode == 1:
|
|
|
# 翻转
|
|
|
im_shadow = im_shadow.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
cut_image = cut_image.transpose(Image.FLIP_LEFT_RIGHT)
|
|
|
+ image_margin = int(padding_800image)
|
|
|
+ bg_size = (1600, 1600)
|
|
|
+ _offset_x, _offset_y = 0, 0
|
|
|
+ scale_rate = 1
|
|
|
+ # im_shadow.show()
|
|
|
+ # =====================主图物体的缩放依据大小
|
|
|
+ if image_margin is not None:
|
|
|
+ _bbox = cut_image.getbbox()
|
|
|
+ _x, _y = _bbox[0], _bbox[1]
|
|
|
+ _w, _h = _bbox[2] - _bbox[0], _bbox[3] - _bbox[1]
|
|
|
+ # 中心偏移量
|
|
|
+ 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))
|
|
|
+ # 计算缩放比例,以及顶点相对位置
|
|
|
+ # print("缩放比例:", scale_rate)
|
|
|
+ # 偏移量
|
|
|
+ _offset_x, _offset_y = offset_x * scale_rate, offset_y * scale_rate
|
|
|
+ # print("偏移量:", _offset_x, _offset_y)
|
|
|
+ # 阴影图缩放尺寸
|
|
|
+ cut_image = to_resize(_im=cut_image, width=cut_image.width * scale_rate)
|
|
|
+ im_shadow = to_resize(_im=im_shadow, width=im_shadow.width * scale_rate)
|
|
|
|
|
|
- # 创建底层背景
|
|
|
- image_bg = Image.new("RGB", (1600, 1600), (255, 255, 255))
|
|
|
+ else:
|
|
|
+ if max_box:
|
|
|
+ im_shadow = to_resize(_im=im_shadow, width=max_box[0], high=max_box[1])
|
|
|
+ cut_image = to_resize(_im=cut_image, width=max_box[0], high=max_box[1])
|
|
|
+ else:
|
|
|
+ size_defind = 1400
|
|
|
+ if resize_mode is None:
|
|
|
+ im_shadow = to_resize(_im=im_shadow, width=size_defind, high=size_defind)
|
|
|
+ cut_image = to_resize(_im=cut_image, width=size_defind, high=size_defind)
|
|
|
+
|
|
|
+ elif resize_mode == 1:
|
|
|
+ im_shadow = to_resize(_im=im_shadow, width=size_defind, high=size_defind)
|
|
|
+ cut_image = to_resize(_im=cut_image, width=size_defind, high=size_defind)
|
|
|
+
|
|
|
+ elif resize_mode == 2:
|
|
|
+ # todo 兼容长筒靴等,将图片大小限制在一个指定的box内
|
|
|
+ im_shadow = to_resize(_im=im_shadow, width=650)
|
|
|
+ cut_image = to_resize(_im=cut_image, width=650)
|
|
|
+ # 再次检查需要约束缩小到一定高度,适应长筒靴
|
|
|
+ _im_x, _im_y = cut_image.size
|
|
|
+ if _im_y > 1400:
|
|
|
+ im_shadow = to_resize(_im=im_shadow, high=1400)
|
|
|
+ cut_image = to_resize(_im=cut_image, high=1400)
|
|
|
+
|
|
|
+ # if im_shadow.height <= im_shadow.width * 1.2:
|
|
|
+ # im_shadow = to_resize(_im=im_shadow, width=650)
|
|
|
+ # cut_image = to_resize(_im=cut_image, width=650)
|
|
|
+ # else:
|
|
|
+ # 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, (255, 255, 255))
|
|
|
+ image_bg = self.paste_img(image=image_bg, top_img=im_shadow, 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_x, image_bg_y = image_bg.size
|
|
|
image_x, image_y = im_shadow.size
|
|
|
|
|
|
@@ -635,3 +742,11 @@ class GeneratePic(object):
|
|
|
if output_queue is not None:
|
|
|
output_queue.put(True)
|
|
|
return True
|
|
|
+ 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
|