SegmentService.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ]
  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)
  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. if x_file_e == ".png":
  58. _is_cutout.append(x_file_name)
  59. # ===============================================================
  60. for file in os.listdir(root_path):
  61. file_path = "{}/{}".format(root_path, file)
  62. if os.path.isdir(file_path):
  63. print(file_path)
  64. # 不是已扣图文件夹,则进行遍历
  65. f = True
  66. for i in self.is_fall_dir:
  67. if i in file:
  68. f = False
  69. break
  70. if f:
  71. self.check_need_cutout_images(file_path)
  72. file_name, file_e = os.path.splitext(file)
  73. if file_e not in _Type:
  74. continue
  75. # 检查文件是否在禁止抠图里
  76. f = True
  77. for i in self.is_fall_file:
  78. if i in file:
  79. f = False
  80. break
  81. if not f:
  82. continue
  83. need_cutout = False if file_name in _is_cutout else True
  84. if os.path.exists("{}/{}.png".format(root_path, file_name)):
  85. need_cutout = False
  86. # 图片进行处理
  87. need_cutout_images.append(
  88. {
  89. "file_name": file_name,
  90. "file_e": file_e,
  91. "file_path": file_path,
  92. "file": file,
  93. "root_path": root_path,
  94. "need_cutout": need_cutout,
  95. }
  96. )
  97. return need_cutout_images