OnePicTest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import threading
  2. import time
  3. import os
  4. import settings
  5. from service.remove_bg_ali import RemoveBgALi
  6. from service.generate_main_image.grenerate_main_image_test import (
  7. GeneratePic,
  8. )
  9. from service.generate_main_image.image_deal_base_func import *
  10. from functools import partial
  11. import io
  12. from PIL import Image
  13. from .base import check_path, get_date_time_original
  14. from middleware import UnicornException
  15. class OnePicTest():
  16. # is_end = Signal()
  17. def __init__(self, pic_path="", is_auto_closed=False):
  18. # super().__init__()
  19. # 加载默认配置
  20. # self.ui = Ui_Form()
  21. # self.ui.setupUi(self)
  22. self.pic_path = pic_path
  23. self.is_auto_closed = False
  24. # self.init()
  25. # self.show()
  26. # if pic_path:
  27. # self.run()
  28. def call_back_deal_image(self, data):
  29. # 处理后回调
  30. print(data)
  31. if data["code"] == 0:
  32. return data
  33. else:
  34. if data["message"]:
  35. raise UnicornException(data["message"])
  36. def deal_image(self, image_path):
  37. # 处理加工图片
  38. return_data = {"code": 99, "message": "", "data": {}}
  39. # =============清空temp文件夹================
  40. check_path("temp")
  41. check_path("temp\pic_test")
  42. root = r"{}\temp\pic_test".format(os.getcwd())
  43. for file_name in os.listdir(root):
  44. path = "{}\{}".format(root, file_name)
  45. if os.path.isfile(path):
  46. os.remove(path)
  47. # ==============抠图处理=====================
  48. remove_pic_ins = RemoveBgALi()
  49. file = os.path.split(image_path)[1]
  50. cut_image_path = r"{}\temp\pic_test\{}.png".format(
  51. os.getcwd(), os.path.splitext(file)[0]
  52. ).replace("\\", "/")
  53. remove_pic_ins.get_image_cut(file_path=image_path, out_file_path=cut_image_path)
  54. # ==============生成主图====================
  55. main_out_path = r"{}\temp\pic_test\{}-主图.jpg".format(
  56. os.getcwd(), os.path.splitext(file)[0]
  57. )
  58. print("image_path", image_path)
  59. print("cut_image_path", cut_image_path)
  60. print("main_out_path", main_out_path)
  61. try:
  62. GeneratePic().run(
  63. image_path=image_path,
  64. cut_image_path=cut_image_path,
  65. out_path=main_out_path,
  66. image_deal_mode=1,
  67. resize_mode=1,
  68. out_pic_size=1024,
  69. is_logo=False,
  70. )
  71. except Exception as e:
  72. print(e)
  73. raise UnicornException("处理失败,请重试")
  74. # return_data["code"] = 0
  75. # return_data["data"]["image_path"] = image_path
  76. # return_data["data"]["image_cutout_path"] = cut_image_path
  77. # return_data["data"]["image_main_path"] = main_out_path
  78. return main_out_path
  79. def HandlerMainImage(self):
  80. # 先做整体校验
  81. if not os.path.exists(self.pic_path):
  82. raise UnicornException("图片不存在")
  83. image_pth = Image.open(self.pic_path)
  84. if image_pth.mode != "RGB":
  85. raise UnicornException("抠图图片不能是PNG")
  86. return_data = self.deal_image(self.pic_path)
  87. # if return_data["message"]:
  88. # raise UnicornException(return_data["message"])
  89. return return_data.replace("\\", "/")
  90. if __name__ == "__main__":
  91. pic = OnePicTest(pic_path="C:/Users/15001/Desktop/mmexport1739929178317.jpg")
  92. pic.HandlerMainImage()