OnePicTest.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. )
  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. GeneratePic().run(
  59. image_path=image_path,
  60. cut_image_path=cut_image_path,
  61. out_path=main_out_path,
  62. image_deal_mode=1,
  63. resize_mode=1,
  64. out_pic_size=1024,
  65. is_logo=False,
  66. )
  67. # return_data["code"] = 0
  68. # return_data["data"]["image_path"] = image_path
  69. # return_data["data"]["image_cutout_path"] = cut_image_path
  70. # return_data["data"]["image_main_path"] = main_out_path
  71. return main_out_path
  72. def HandlerMainImage(self):
  73. # 先做整体校验
  74. if not os.path.exists(self.pic_path):
  75. raise UnicornException("图片不存在")
  76. return_data = self.deal_image(self.pic_path)
  77. if return_data["message"]:
  78. raise UnicornException(return_data["message"])
  79. return return_data
  80. if __name__ == "__main__":
  81. pic = OnePicTest(pic_path="C:/Users/15001/Desktop/mmexport1739929178317.jpg")
  82. pic.HandlerMainImage()