detial_hqt.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """
  2. 步骤:
  3. 1、整理需要处理的款号图-输出款号图文件夹
  4. 2、整理所有相关的图片作为素材图
  5. 3、按要求进行拼接
  6. """
  7. import os
  8. from PIL import ImageFont
  9. from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
  10. from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
  11. class DetailPicGetHqt(DetailBase):
  12. need_view = ["俯视","侧视","后跟","鞋底","内里"]
  13. root = r"{}\resources\detail_temp\hongqingting\1".format(os.getcwd())
  14. def __init__(self, goods_no, goods_no_value: dict, out_put_dir, windows=None, test=False, excel_data=None,
  15. assigned_page_list=None,**kwargs):
  16. super().__init__(goods_no, goods_no_value, out_put_dir,windows=windows)
  17. self.root = r"{}\resources\detail_temp\hongqingting\1".format(os.getcwd())
  18. self.run()
  19. def run(self):
  20. detailed_images = self.deal_details()
  21. self.create_folder(self.out_put_dir)
  22. detail_path = "{out_put_dir}/{goods_no}/details".format(out_put_dir=self.out_put_dir, goods_no=self.goods_no)
  23. self.create_folder(detail_path)
  24. join_path = "{out_put_dir}/{goods_no}/拼接图".format(out_put_dir=self.out_put_dir, goods_no=self.goods_no)
  25. self.create_folder(join_path)
  26. for index, pp in enumerate(detailed_images):
  27. pp.im.save("{}/{}({}).jpg".format(detail_path, self.goods_no, str(index + 11).zfill(2)), format="JPEG")
  28. img = self.add_pic(detailed_images)
  29. img.save("{}/1.jpg".format(join_path), format="JPEG")
  30. # ------------移动其他图片---------------------
  31. self.move_other_pic()
  32. return True
  33. def deal_details(self):
  34. bg_color = (246, 246, 246)
  35. self.image_init(bg_color)
  36. detailed_images = []
  37. detailed_images.append(self.deal_pic_1())
  38. detailed_images.append(self.deal_pic_2())
  39. detailed_images.append(self.deal_pic_3())
  40. detailed_images.append(self.deal_pic_4())
  41. detailed_images.append(self.deal_pic_5())
  42. return [x for x in detailed_images if x]
  43. def deal_pic_1(self):
  44. bg_color = (246, 246, 246)
  45. """ 制作主图 """
  46. goods_art_no_list = list(self.data.keys())
  47. pp_1 = self.data[goods_art_no_list[0]]["pic_is_deal"]["侧视"]
  48. pp_image = pp_1.resize(mode="relative",
  49. base="by_im",
  50. value=0,
  51. base_im=PictureProcessing("RGB", (550, 340), (248, 248, 248)),
  52. percentage=0)
  53. pp_image = PictureProcessing("RGB", (550, 340), bg_color).paste_img(
  54. mode="pixel", top_img=pp_image, base="center"
  55. )
  56. bg_img = PictureProcessing(r"{}\image (1).jpg".format(self.root)).paste_img(
  57. mode="pixel", top_img=pp_image, base="nw", value=(26, 263))
  58. # bg_img.show()
  59. # ---------- 第二张图粘贴
  60. pp_2 = self.data[goods_art_no_list[0]]["pic_is_deal"]["俯视"]
  61. pp_image = pp_2.resize(mode="relative",
  62. base="by_im",
  63. value=0,
  64. base_im=PictureProcessing("RGB", (550, 340), (248, 248, 248)),
  65. percentage=0)
  66. pp_image = PictureProcessing("RGB", (550, 340), bg_color).paste_img(
  67. mode="pixel", top_img=pp_image, base="center"
  68. )
  69. bg_img = bg_img.paste_img(
  70. mode="pixel", top_img=pp_image, base="nw", value=(26, 626))
  71. # -------粘贴文字-------
  72. font = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
  73. text_list = [("商品款号", self.goods_no_value["款号"], 700, 297),
  74. ("商品面料", self.goods_no_value["商品面料"], 700, 380),
  75. ("商品内里", self.goods_no_value["商品内里"], 700, 469),
  76. ("商品鞋底", self.goods_no_value["商品鞋底"], 700, 549),
  77. ("后帮高", self.goods_no_value["后帮高"], 700, 667),
  78. ("前掌宽", self.goods_no_value["前掌宽"], 700, 751),
  79. ("鞋跟高", self.goods_no_value["鞋跟高"], 700, 828), ]
  80. for i in text_list:
  81. bg_img = bg_img.add_text(mode="pixel",
  82. value=(i[2], i[3]),
  83. font=font,
  84. text="{}:{}".format(i[0], i[1]),
  85. align="center",
  86. spacing=10,
  87. fill=(17, 16, 16))
  88. return bg_img
  89. def deal_pic_2(self):
  90. # 设计师说展示图
  91. # 设计师说
  92. if "FAB说明" not in self.goods_no_value:
  93. return
  94. text = self.goods_no_value["FAB说明"] # FAB说明
  95. if not text:
  96. return
  97. pp_1 = PictureProcessing(r"{}\image (2).jpg".format(self.root))
  98. text = text.replace(r"\n", "\n")
  99. _ = text.split("\n")
  100. text_img = PictureProcessing("RGB", (1200, len(_) * 100 + 30), (255, 255, 255))
  101. font = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
  102. text_img = text_img.add_text(mode="pixel",
  103. value=(600, 50),
  104. font=font,
  105. text=text,
  106. anchor="ma",
  107. align="center",
  108. spacing=50,
  109. fill=(83, 83, 83))
  110. new_image = PictureProcessing("RGB", (1200, pp_1.height + text_img.height), (255, 255, 255))
  111. new_image = new_image.paste_img(mode="pixel", top_img=pp_1, value=(0, 0))
  112. new_image = new_image.paste_img(mode="pixel", top_img=text_img, value=(0, pp_1.height))
  113. return new_image
  114. def deal_pic_3(self):
  115. # 制作角度展示图
  116. bg_color = (246, 246, 246)
  117. detailed_images = []
  118. pp_1 = PictureProcessing(r"{}\image (3).jpg".format(self.root))
  119. detailed_images.append(pp_1)
  120. goods_art_no_list = list(self.data.keys())
  121. for index, goods_art_no in enumerate(goods_art_no_list):
  122. if index == 0:
  123. name_list = ["侧视", "俯视", "后跟", "鞋底"]
  124. else:
  125. name_list = ["侧视", "俯视", ]
  126. for _name in name_list:
  127. # 处理图片,需要粘贴到背景等处理
  128. if _name not in self.data[goods_art_no]["pic_is_deal"]:
  129. continue
  130. _pp = self.data[goods_art_no]["pic_is_deal"][_name]
  131. _pp = _pp.resize(mode="pixel", base="width", value=649 if _name == "后跟" else 1058, )
  132. bg_img = PictureProcessing("RGBA", (1200, int(_pp.height + 50)), bg_color)
  133. bg_img = bg_img.paste_img(mode="pixel", top_img=_pp, base="center")
  134. detailed_images.append(bg_img)
  135. # 添加分割线
  136. detailed_images.append(PictureProcessing("RGB", (1200, 50), (255, 255, 255)))
  137. return PictureProcessing(im=self.add_pic(detailed_images))
  138. def deal_pic_4(self):
  139. # 制作详细图
  140. bg_color = (246, 246, 246)
  141. detailed_images = []
  142. pp_1 = PictureProcessing(r"{}\image (4).jpg".format(self.root))
  143. detailed_images.append(pp_1)
  144. name_list = ["俯视", "内里", "后跟", "鞋底"]
  145. goods_art_no = list(self.data.keys())[0]
  146. view_dict = {"俯视": (0, 0, 1100, 1200),
  147. "内里": (-1, -100, 1100, 1200),
  148. "后跟": (0, -100, 1100, 1200),
  149. "鞋底": (0, -100, 1100, 1200),
  150. }
  151. for _name in name_list:
  152. # 处理图片,需要粘贴到背景等处理
  153. if _name not in self.data[goods_art_no]["pic_is_deal"]:
  154. continue
  155. _pp = self.data[goods_art_no]["pic_is_deal"][_name]
  156. _pp = _pp.resize(mode="pixel", base="width", value=1300 if _name == "后跟" else 2200, )
  157. _pp = _pp.crop_img(mode="pixel", base="sw", value=view_dict[_name], color_fill=bg_color)
  158. _pp = _pp.radius(mode="relative", circular_pos=(1, 0, 0, 1), value=100)
  159. bg_pp = PictureProcessing("RGB", (1200, 1316), (255, 255, 255))
  160. bg_pp.paste_img(mode="pixel", top_img=_pp, base="ec", value=(0, 0))
  161. detailed_images.append(bg_pp)
  162. return PictureProcessing(im=self.add_pic(detailed_images))
  163. def deal_pic_5(self):
  164. # 其他图片添加
  165. pp_1 = PictureProcessing(r"{}\image (5).jpg".format(self.root))
  166. return pp_1
  167. if __name__ == '__main__':
  168. import json
  169. with open(
  170. r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\qt_test\data2.txt",
  171. "r", encoding="utf-8") as f:
  172. data = json.loads(f.read())
  173. for goods_no, value in data.items():
  174. d = DetailPicGetHqt(goods_no, value, out_put_dir=r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\out_put\测试文件夹\详情图生成")
  175. raise 1