Selaa lähdekoodia

白底图处理

rambo 11 kuukautta sitten
vanhempi
commit
48ced010e5
1 muutettua tiedostoa jossa 57 lisäystä ja 3 poistoa
  1. 57 3
      python/services/deal_one_image.py

+ 57 - 3
python/services/deal_one_image.py

@@ -11,11 +11,26 @@ from .other.remove_bg_ali import RemoveBgALi
 import cv2
 import numpy as np
 from middleware import UnicornException
+import math
+
+
+def disposeWH(image: Image, base_size=1024):
+    width, height = image.size
+    scale_width = int(width) 
+    scale_height = int(height)
+    scale_rate = scale_width / scale_height
+    if scale_width > base_size:
+        scale_width = base_size
+        scale_height = int(math.floor(scale_width / scale_rate))
+    if scale_height > base_size:
+        scale_height = base_size
+        scale_width = int(math.floor(scale_height * scale_rate))
+    return scale_width, scale_height
 
 
 class Base(object):
 
-    def __init__(self, image_data, lock, windows, num,token):
+    def __init__(self, image_data, lock, windows, num, token):
         self.lock = lock
         self.image_data = image_data
         self.num = num
@@ -122,6 +137,7 @@ class DealOneImage(Base):
     def __init__(self, image_data, lock, windows, num, token):
         super().__init__(image_data, lock, windows, num, token)
         self.r_pixian = RemoveBgPiXian()
+        self.windows = windows
 
     def run(self, image_data, upload_pic_dict):
         self.file_path = image_data["file_path"]
@@ -183,8 +199,17 @@ class DealOneImage(Base):
                 ),
             )
             if self.windows.output_type == 1:
+                print("处理白底","ssssss")
                 background = Image.new("RGBA", _img_im.size, (255, 255, 255, 255))
                 _img_im = Image.alpha_composite(background, _img_im)
+                resize_width, resize_height = disposeWH(_img_im.convert("RGB"), 1024)
+                _img_im = _img_im.resize((resize_width, resize_height))
+                new_bg = Image.new("RGB", (1024, 1024), (255, 255, 255))
+                _img_im = _img_im.convert("RGB").copy()
+                width, height = _img_im.size
+                offset = ((1024 - width) // 2, (1024 - height) // 2)
+                new_bg.paste(_img_im, offset)
+                _img_im = new_bg.copy()
             cut_status = True
             success_path = out_path
             _img_im.save(out_path)
@@ -258,6 +283,17 @@ class DealOneImage(Base):
                 text = "抠图异常,code:{}".format(pixian_cutout_data["status_code"])
             self.add_log(text)
             return None
+        # if self.windows.output_type == 1:
+        #     resize_width, resize_height = disposeWH(
+        #         second_cut_image.convert("RGB"), 1024
+        #     )
+        #     second_cut_image = second_cut_image.resize((resize_width, resize_height))
+        #     new_bg = Image.new("RGB", (1024, 1024), (255, 255, 255))
+        #     second_cut_image = second_cut_image.convert("RGB").copy()
+        #     width, height = second_cut_image.size
+        #     offset = ((1024 - width) // 2, (1024 - height) // 2)
+        #     new_bg.paste(second_cut_image, offset)
+        #     second_cut_image = new_bg.copy()
         return second_cut_image
 
     def cutoutFail(self, pixian_cutout_data):
@@ -416,8 +452,8 @@ class DealOneImageBeforehand(Base):
             raise UnicornException("次数不足,处理失败")
         self.get_online_data.dispose_point("sub")
         cut_image = self.r_ali.get_image_cut(
-                file_path=None, out_file_path=None, original_im=original_pic.im
-            )
+            file_path=None, out_file_path=None, original_im=original_pic.im
+        )
         success_path = ""
         if cut_image is None:
             cut_status = False
@@ -430,6 +466,14 @@ class DealOneImageBeforehand(Base):
             if self.windows.output_type == 1:
                 background = Image.new("RGBA", cut_image.size, (255, 255, 255, 255))
                 cut_image = Image.alpha_composite(background, cut_image)
+                resize_width, resize_height = disposeWH(cut_image.convert("RGB"), 1024)
+                cut_image = cut_image.resize((resize_width, resize_height))
+                new_bg = Image.new("RGB", (1024, 1024), (255, 255, 255))
+                cut_image = cut_image.convert("RGB").copy()
+                width, height = cut_image.size
+                offset = ((1024 - width) // 2, (1024 - height) // 2)
+                new_bg.paste(cut_image, offset)
+                cut_image = new_bg.copy()
             success_path = "{}/{}.png".format(save_root_path, self.file_name)
             cut_image.save(success_path)
         image_data["status"] = cut_status
@@ -475,6 +519,16 @@ class DealOneImageBeforehand(Base):
                 if self.windows.output_type == 1:
                     background = Image.new("RGBA", image_obj.size, (255, 255, 255, 255))
                     image_obj = Image.alpha_composite(background, image_obj)
+                    resize_width, resize_height = disposeWH(
+                        image_obj.convert("RGB"), 1024
+                    )
+                    image_obj = image_obj.resize((resize_width, resize_height))
+                    new_bg = Image.new("RGB", (1024, 1024), (255, 255, 255))
+                    image_obj = image_obj.convert("RGB").copy()
+                    width, height = image_obj.size
+                    offset = ((1024 - width) // 2, (1024 - height) // 2)
+                    new_bg.paste(image_obj, offset)
+                    image_obj = new_bg.copy()
                 image_obj.save("{}/{}.png".format(save_file_path, cn_name))
         image_data["status"] = cut_status
         image_data["success_path"] = success_path