detail_xiaosushuoxie.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 DetailPicGetXiaoSuShuoXie(DetailBase):
  12. need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
  13. root = r"{}\resources\detail_temp\xiaosushuoxie\1".format(os.getcwd())
  14. def __init__(self, goods_no, goods_no_value: dict, out_put_dir, windows=None, test=False,excel_data=None,assigned_page_list=None):
  15. super().__init__(goods_no, goods_no_value, out_put_dir, windows=windows,excel_data=excel_data,assigned_page_list=assigned_page_list)
  16. self.root = r"{}\resources\detail_temp\xiaosushuoxie\1".format(os.getcwd())
  17. self.deal_pic_func_list = [
  18. self.deal_pic_1,
  19. self.deal_pic_2,
  20. self.deal_pic_3,
  21. self.deal_pic_4,
  22. self.deal_pic_5,
  23. ]
  24. if test:
  25. self.run_test()
  26. else:
  27. self.run_all()
  28. def run_test(self):
  29. detailed_images = []
  30. detailed_images.append(self.deal_pic_1())
  31. detailed_images.append(self.deal_pic_2())
  32. detailed_images.append(self.deal_pic_3())
  33. detailed_images.append(self.deal_pic_4())
  34. detailed_images.append(self.deal_pic_5())
  35. img = self.add_pic(detailed_images)
  36. img.save(r"{}/{}.jpg".format(self.out_put_dir,self.goods_no, format="JPEG"))
  37. def deal_pic_1(self):
  38. bg_color = (239, 237, 238)
  39. """ 制作主图 """
  40. detailed_images = []
  41. # -------粘贴文字-------
  42. font = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
  43. text_list = []
  44. for _set in [("【品 牌】", "品牌"),
  45. ("【货 号】", "款号"),
  46. ("【鞋面材质】", "鞋面材质"),
  47. ("【系列风格】", "系列风格"),
  48. ("【内里材质】", "内里材质"),
  49. ("【尺 码】", "尺码"),
  50. ("【鞋底材质】", "鞋底材质"),
  51. ("【品牌编码】", "品牌编码"),
  52. ]:
  53. title, key = _set
  54. text = self.get_text_value(key)
  55. if text:
  56. text_list.append(
  57. (title, text)
  58. )
  59. # 文字排列
  60. pp_bg = PictureProcessing(r"{}\bg-1.jpg".format(self.root))
  61. x = 101
  62. y = 98
  63. for _set in text_list:
  64. pp_bg = pp_bg.add_text(mode="pixel",
  65. value=(x, y),
  66. font=font,
  67. text="{}:{}".format(_set[0], _set[1]),
  68. align="left",
  69. spacing=10,
  70. fill=(17, 16, 16))
  71. x += 588
  72. if x > 700:
  73. x = 101
  74. y += 94
  75. # ----------主图放置
  76. y += 10
  77. goods_art_no_list = list(self.data.keys())
  78. pp_jpg, pp_png = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="俯视", return_orign=True)
  79. pp_jpg = pp_jpg.resize(value=1083)
  80. pp_png = pp_png.resize(value=1083)
  81. pp_bg = pp_bg.to_overlay_pic_advance(mode="pixel", top_img=pp_jpg, base="nc", value=(0, y), top_png_img=pp_png)
  82. y += pp_jpg.height
  83. y += 20
  84. # ----------配图粘贴
  85. if len(goods_art_no_list) > 1:
  86. # 大于一个颜色做颜色图展示
  87. pp_list_1,pp_list_2 = [],[]
  88. for goods_art_no in goods_art_no_list:
  89. pp_jpg, pp_png = self.image_one_pic(goods_art_no=goods_art_no, name="侧视", return_orign=True)
  90. pp_jpg = pp_jpg.resize(value=400)
  91. pp_png = pp_png.resize(value=400)
  92. pp_list_1.append(pp_jpg)
  93. pp_list_2.append(pp_png)
  94. _pp_jpg = PictureProcessing()
  95. _pp_png = PictureProcessing()
  96. _pp_jpg = _pp_jpg.horizontal_distribution(pp_list=pp_list_1, bg_width=1200, margins=(0, 0, 0, 0), line_spacing=0,
  97. number_per_row=3)
  98. _pp_png = _pp_png.horizontal_distribution(pp_list=pp_list_2, bg_width=1200, margins=(0, 0, 0, 0), line_spacing=0,
  99. number_per_row=3)
  100. pp_bg = pp_bg.to_overlay_pic_advance(mode="pixel", top_img=_pp_jpg, base="nc", value=(0, y),
  101. top_png_img=_pp_png)
  102. y += _pp_jpg.height
  103. y += 30
  104. # ------多余的剪裁
  105. if y < pp_bg.height:
  106. # print("触发剪裁")
  107. pp_bg = pp_bg.crop_img(value=(0, 0, pp_bg.width, y))
  108. return pp_bg
  109. def deal_pic_2(self):
  110. """
  111. 细节解析
  112. """
  113. font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
  114. font_2 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 15)
  115. bg_color = (220, 220, 220)
  116. detailed_images = []
  117. goods_art_no_list = list(self.data.keys())
  118. # ------侧视图-------------
  119. pp_2 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="侧视", bg_color=bg_color)
  120. pp_2 = pp_2.resize(mode="pixel", base="width", value=1327)
  121. pp_2 = PictureProcessing("RGB", (550, 550), bg_color).paste_img(mode="pixel", top_img=pp_2,
  122. base="wc", value=(-90, 0))
  123. # 文字
  124. pp_2 = pp_2.add_text(mode="pixel",
  125. value=(21, 24),
  126. font=font_1,
  127. text="优质质感鞋面",
  128. align="left",
  129. spacing=10,
  130. fill=(28, 28, 28))
  131. pp_2 = pp_2.add_text(mode="pixel",
  132. value=(21, 76),
  133. font=font_2,
  134. text="颇具层次",
  135. align="left",
  136. spacing=10,
  137. fill=(78, 78, 78))
  138. pp_2 = pp_2.add_text(mode="pixel",
  139. value=(21, 115),
  140. font=font_2,
  141. text="时尚质感出众",
  142. align="left",
  143. spacing=10,
  144. fill=(78, 78, 78))
  145. # ------后跟-------------
  146. pp_3 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="后跟", bg_color=bg_color)
  147. pp_3 = pp_3.resize(mode="pixel", base="width", value=625)
  148. pp_3 = PictureProcessing("RGB", (550, 550), bg_color).paste_img(mode="pixel", top_img=pp_3,
  149. base="cc", value=(0, 0))
  150. # 文字
  151. pp_3 = pp_3.add_text(mode="pixel",
  152. value=(pp_3.width - 21, pp_3.height - 35 * 3),
  153. font=font_2,
  154. text="颇具层次",
  155. align="right",
  156. spacing=10,
  157. anchor="rs",
  158. fill=(78, 78, 78))
  159. pp_3 = pp_3.add_text(mode="pixel",
  160. value=(pp_3.width - 21, pp_3.height - 35 * 2),
  161. font=font_2,
  162. text="时尚质感出众",
  163. align="right",
  164. anchor="rs",
  165. spacing=10,
  166. fill=(78, 78, 78))
  167. pp_3 = pp_3.add_text(mode="pixel",
  168. value=(pp_3.width - 21, pp_3.height - 35),
  169. font=font_1,
  170. text="设计感十足",
  171. anchor="rs",
  172. align="right",
  173. spacing=10,
  174. fill=(28, 28, 28))
  175. # ---------------鞋底图
  176. pp_4 = self.image_one_pic(goods_art_no=goods_art_no_list[0], name="鞋底", bg_color=bg_color)
  177. pp_4 = pp_4.resize(mode="pixel", base="width", value=1635)
  178. pp_4 = PictureProcessing("RGB", (1134, 625), bg_color).paste_img(mode="pixel", top_img=pp_4,
  179. base="wc", value=(198, 0))
  180. # 文字
  181. pp_4 = pp_4.add_text(mode="pixel",
  182. value=(21, 451),
  183. font=font_1,
  184. text="防滑耐磨鞋底",
  185. align="left",
  186. spacing=10,
  187. fill=(28, 28, 28))
  188. pp_4 = pp_4.add_text(mode="pixel",
  189. value=(21, 510),
  190. font=font_2,
  191. text="优质材质",
  192. align="left",
  193. spacing=10,
  194. fill=(78, 78, 78))
  195. pp_4 = pp_4.add_text(mode="pixel",
  196. value=(21, 549),
  197. font=font_2,
  198. text="防滑网纹设计坚固耐磨",
  199. align="left",
  200. spacing=10,
  201. fill=(78, 78, 78))
  202. new_bg = PictureProcessing("RGB", (1200, 40 + pp_2.height + 40 + pp_4.height + 40), (239, 239, 239))
  203. new_bg = new_bg.paste_img(mode="pixel", top_img=pp_2, base="nw", value=(33, 40))
  204. new_bg = new_bg.paste_img(mode="pixel", top_img=pp_3, base="nw", value=(33 + 550 + 33, 40))
  205. new_bg = new_bg.paste_img(mode="pixel", top_img=pp_4, base="nw", value=(33, 40 + pp_2.height + 40))
  206. detailed_images.append(new_bg)
  207. return PictureProcessing(im=self.add_pic(detailed_images))
  208. def deal_pic_3(self):
  209. # 设计理念
  210. bg = PictureProcessing(r"{}\1 (1).jpg".format(self.root))
  211. font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 40)
  212. # 粘贴文字
  213. text = self.get_text_value("设计理念")
  214. if not text:
  215. text = "告别枯燥沉闷日常还原自然本身色彩\n融合工装、复古、运动元素随性出走感受自在的步伐"
  216. bg = bg.add_text(mode="pixel",
  217. value=(164, 328),
  218. font=font_1,
  219. text=text,
  220. align="left",
  221. spacing=15,
  222. fill=(39, 39, 39))
  223. return bg
  224. def deal_pic_4(self):
  225. """ 各个颜色细节展示 """
  226. bg_color = (255, 255, 255)
  227. detailed_images = []
  228. goods_art_no_list = list(self.data.keys())
  229. font_1 = ImageFont.truetype(r'resources\ttf\simhei.ttf', 30)
  230. for goods_art_no_dict in self.goods_no_value["货号资料"]:
  231. color_name = goods_art_no_dict["颜色名称"]
  232. goods_art_no = goods_art_no_dict["货号"]
  233. # ------俯视图-------------
  234. pp_1 = self.image_one_pic(goods_art_no=goods_art_no, name="俯视", bg_color=bg_color)
  235. pp_1 = pp_1.resize(mode="pixel", base="width", value=956)
  236. pp_1 = PictureProcessing("RGB", (1200, 40 + pp_1.height + 40), bg_color).paste_img(mode="pixel",
  237. top_img=pp_1,
  238. base="cc", value=(0, 0))
  239. # 粘贴文字与logo
  240. pp_1 = pp_1.add_text(mode="pixel",
  241. value=(51, 26),
  242. font=font_1,
  243. text=color_name,
  244. align="left",
  245. spacing=10,
  246. fill=(28, 28, 28))
  247. pp_1 = pp_1.paste_img(mode="pixel", top_img=PictureProcessing(
  248. r"{}\1 (4).jpg".format(self.root)), base="nw", value=(51, 72))
  249. # ------后跟-------------
  250. pp_2 = self.image_one_pic(goods_art_no=goods_art_no, name="后跟", bg_color=bg_color)
  251. pp_2 = pp_2.resize(mode="pixel", base="width", value=345)
  252. pp_2 = PictureProcessing("RGB", (537, 356), bg_color).paste_img(mode="pixel", top_img=pp_2,
  253. base="cc", value=(0, 0))
  254. # ------内里-------------
  255. pp_3 = self.image_one_pic(goods_art_no=goods_art_no, name="内里", bg_color=bg_color)
  256. pp_3 = pp_3.resize(mode="pixel", base="width", value=577)
  257. pp_3 = PictureProcessing("RGB", (537, 356), bg_color).paste_img(mode="pixel", top_img=pp_3,
  258. base="cc", value=(0, 0))
  259. bg_pp = PictureProcessing("RGB", (1200, 20 + pp_1.height + 20 + pp_2.height + 20), bg_color)
  260. # 粘贴俯视图
  261. bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_1, base="nw", value=(0, 0))
  262. # 粘贴后跟
  263. bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_2, base="nw", value=(51, 20 + pp_1.height + 20))
  264. # 粘贴内里
  265. bg_pp = bg_pp.paste_img(mode="pixel", top_img=pp_3, base="nw", value=(588, 20 + pp_1.height + 20))
  266. detailed_images.append(bg_pp)
  267. return PictureProcessing(im=self.add_pic(detailed_images))
  268. def deal_pic_5(self):
  269. # 其他图片
  270. detailed_images = []
  271. detailed_images.append(
  272. PictureProcessing(r"{}\1 (2-1).jpg".format(self.root)))
  273. detailed_images.append(
  274. PictureProcessing(r"{}\1 (3).jpg".format(self.root)))
  275. return PictureProcessing(im=self.add_pic(detailed_images))
  276. if __name__ == '__main__':
  277. import json
  278. with open(
  279. r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\qt_test\data3.txt",
  280. "r", encoding="utf-8") as f:
  281. data = json.loads(f.read())
  282. for goods_no, value in data.items():
  283. d = DetailPicGetXiaoSuShuoXie(goods_no, value,
  284. out_put_dir=r"D:\MyDocuments\PythonCode\MyPython\red_dragonfly\deal_pics\auto_capture_V2\auto_photo\output\2024-11-19\软件-详情图生成")
  285. raise 1