Browse Source

图片句柄释放问题

rambo 1 month ago
parent
commit
2a1987911a

+ 12 - 3
python/service/grenerate_main_image_test.py

@@ -11,7 +11,7 @@ from functools import wraps
 from .multi_threaded_image_saving import ImageSaver
 from .get_mask_by_green import GetMask
 from middleware import UnicornException
-
+from logger import logger
 def time_it(func):
     @wraps(func)  # 使用wraps来保留原始函数的元数据信息
     def wrapper(*args, **kwargs):
@@ -474,7 +474,6 @@ class GeneratePic(object):
         _s = time.time()
         orign_im = Image.open(image_path)  # 原始图
         print("242  need_time_1:{}".format(time.time() - _s))
-
         orign_x, orign_y = orign_im.size
         cut_image = Image.open(cut_image_path)  # 原始图的已扣图
         cut_image, new_box = get_mini_crop_img(img=cut_image)
@@ -738,7 +737,17 @@ class GeneratePic(object):
                     _format=new_format,
                 )
                 # image_bg.save(out_path)
-
+        # 在函数结束时使用更安全的关闭方式
+        try:
+            if 'orign_im' in locals() and orign_im:
+                orign_im.close()
+            if 'cut_image' in locals() and cut_image:
+                cut_image.close()
+            if 'logo_im' in locals() and logo_im:
+                logo_im.close()
+        except Exception as e:
+            logger.info(f"阴影处理关闭图片对象失败:{str(e)}")
+            print(f"Error closing images: {e}")
         if output_queue is not None:
             output_queue.put(True)
         return True

+ 2 - 0
python/service/image_pic_deal.py

@@ -32,6 +32,7 @@ class OnePicDeal(object):
             print("自动识别----->这是左脚")
         else:
             print("自动识别----->这是右脚")
+        im.close()
         return flag
 
     def check_shoe_is_right_by_pixel(self, im=None, image_path=None):
@@ -57,6 +58,7 @@ class OnePicDeal(object):
                 left_f_num += 1
             else:
                 left_f_num -= 1
+        im.close()
         if left_f_num > 0:
             return True
         else:

+ 1 - 0
python/service/multi_threaded_image_saving.py

@@ -87,6 +87,7 @@ class ImageSaver:
                     image.save(out_path, quality=quality, format=_format)
             else:
                 image.save(out_path, format=_format)
+        image.close()
 
     def get_completed_images(self, file_path):
         """

+ 1 - 1
python/service/online_request/module_online_data.py

@@ -112,7 +112,7 @@ class AIGCDataRequest(object):
             # 如果保存为JPG等不支持透明度的格式,转换为RGB并使用白色背景
             background = background.convert("RGB")
             background.save(output_path)
-
+        source_img.close()
         return background
 
     def generateProductScene(self, local_path, prompt, save_path):