SegmentService.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import os
  2. from middleware import UnicornException
  3. class SegmentService:
  4. is_fall_file = ["扣图", "已扣图"]
  5. is_fall_dir = ["微信"]
  6. def initImages(self, image_list):
  7. need_cutout_images = []
  8. for image_path in image_list:
  9. root_path, file = os.path.split(image_path)
  10. file_name, file_e = os.path.splitext(file)
  11. need_cutout_images.append(
  12. {
  13. "file_name": file_name,
  14. "file_e": file_e,
  15. "file_path": image_path,
  16. "file": file,
  17. "root_path": root_path,
  18. "need_cutout": True,
  19. }
  20. )
  21. for index, image_data in enumerate(need_cutout_images):
  22. root_path = image_data["root_path"]
  23. file_name = image_data["file_name"]
  24. _path_1 = "{}/{}.png".format(root_path, file_name)
  25. _path_2 = "{}/已扣图/{}.png".format(root_path, file_name)
  26. if os.path.exists(_path_1):
  27. need_cutout_images[index]["need_cutout"] = False
  28. continue
  29. if os.path.exists(_path_2):
  30. need_cutout_images[index]["need_cutout"] = False
  31. continue
  32. return need_cutout_images
  33. def check_need_cutout_images(self, root_path):
  34. _n_c = 0
  35. need_cutout_images = []
  36. _Type = [
  37. ".jpg",
  38. ".JPG",
  39. ".jpeg",
  40. ".JPEG",
  41. ".png",
  42. ".PNG",
  43. ]
  44. _is_cutout = []
  45. if not os.path.isdir(root_path):
  46. raise UnicornException("不是有效路径")
  47. for file in os.listdir(root_path):
  48. _n_c += 1
  49. if _n_c > 500:
  50. need_cutout_images = []
  51. raise UnicornException("目录下文件过多,请检查目录是否正确")
  52. file_path = "{}/{}".format(root_path, file)
  53. if os.path.isdir(file_path):
  54. print(file_path)
  55. if file == "已扣图":
  56. # 哪些图片已经有抠图
  57. for x_file in os.listdir(file_path):
  58. x_file_name, x_file_e = os.path.splitext(x_file)
  59. if x_file_e == ".png":
  60. continue
  61. _is_cutout.append(x_file_name)
  62. # ===============================================================
  63. for file in os.listdir(root_path):
  64. file_path = "{}/{}".format(root_path, file)
  65. if os.path.isdir(file_path):
  66. print(file_path)
  67. # 不是已扣图文件夹,则进行遍历
  68. f = True
  69. for i in self.is_fall_dir:
  70. if i in file:
  71. f = False
  72. break
  73. if f:
  74. self.check_need_cutout_images(file_path)
  75. file_name, file_e = os.path.splitext(file)
  76. if file_e not in _Type:
  77. continue
  78. # 检查文件是否在禁止抠图里
  79. f = True
  80. for i in self.is_fall_file:
  81. if i in file:
  82. f = False
  83. break
  84. if not f:
  85. continue
  86. need_cutout = False if file_name in _is_cutout else True
  87. # if os.path.exists("{}/{}.png".format(root_path, file_name)):
  88. # need_cutout = False
  89. # 图片进行处理
  90. need_cutout_images.append(
  91. {
  92. "file_name": file_name,
  93. "file_e": file_e,
  94. "file_path": file_path,
  95. "file": file,
  96. "root_path": root_path,
  97. "need_cutout": need_cutout,
  98. }
  99. )
  100. return need_cutout_images