SegmentService.py 3.9 KB

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