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