detail_xiaosushuoxie4.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. """
  2. 步骤:
  3. 1、整理需要处理的款号图-输出款号图文件夹
  4. 2、整理所有相关的图片作为素材图
  5. 3、按要求进行拼接
  6. """
  7. import os
  8. import settings
  9. from PIL import ImageFont
  10. # from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
  11. # from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
  12. is_test_plugins = False
  13. try:
  14. is_test_plugins = settings.is_test_plugins
  15. except:
  16. is_test_plugins = False
  17. if is_test_plugins:
  18. from custom_plugins.plugins_mode.detail_generate_base import DetailBase
  19. from custom_plugins.plugins_mode.pic_deal import PictureProcessing
  20. else:
  21. from plugins_mode.detail_generate_base import DetailBase
  22. from plugins_mode.pic_deal import PictureProcessing
  23. plugins_name = "详情模板"
  24. company_name_list = ["小苏"]
  25. template_name = "xiaosushuoxie-4"
  26. class DetailPicGet(DetailBase):
  27. need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
  28. root = r"{}\resources\detail_temp\xiaosushuoxie\4".format(os.getcwd())
  29. def __init__(self, goods_no, goods_no_value: dict, out_put_dir, windows=None, test=False, excel_data=None,
  30. assigned_page_list=None):
  31. super().__init__(goods_no, goods_no_value, out_put_dir, windows=windows, excel_data=excel_data,
  32. assigned_page_list=assigned_page_list)
  33. self.template_name = template_name
  34. self.root = r"{}\resources\detail_temp\xiaosushuoxie\4".format(os.getcwd())
  35. print("run xiaosushuoxie-4 ")
  36. self.base_bg_color = (255, 255, 255)
  37. self.deal_pic_func_list = [
  38. self.deal_pic_1,
  39. self.deal_pic_2,
  40. self.deal_pic_3,
  41. self.deal_pic_4,
  42. self.deal_pic_5,
  43. self.deal_pic_6,
  44. self.deal_pic_7,
  45. ]
  46. if test:
  47. # for k, v in self.goods_no_value.items():
  48. # print(k, v)
  49. self.run_test()
  50. else:
  51. self.run_all()
  52. def run_test(self):
  53. detailed_images = []
  54. detailed_images.append(self.deal_pic_1())
  55. # detailed_images.append(self.deal_pic_2())
  56. # detailed_images.append(self.deal_pic_3())
  57. # detailed_images.append(self.deal_pic_4())
  58. # detailed_images.append(self.deal_pic_5())
  59. # detailed_images.append(self.deal_pic_6())
  60. # detailed_images.append(self.deal_pic_7())
  61. img = self.add_pic(detailed_images)
  62. # img.save(r"{}/{}.jpg".format(self.out_put_dir, self.goods_no, format="JPEG"))
  63. img.show()
  64. # 主图
  65. def deal_pic_1(self):
  66. """ 制作主图 """
  67. detailed_images = []
  68. pp_bg = PictureProcessing(r"{}\t (1).png".format(self.root))
  69. # 添加分割线
  70. # detailed_images.append(PictureProcessing("RGB", (1200, 100), (255, 255, 255)))
  71. detailed_images.append(pp_bg)
  72. # -------粘贴文字-------
  73. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 56)
  74. font_2 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 26)
  75. text_list = []
  76. text_list.append({"text": self.get_text_value("标题"),
  77. "font": font_1,
  78. "fill": (17, 16, 16),
  79. })
  80. text_list.append({"text": self.get_text_value("副标题"),
  81. "font": font_2,
  82. "fill": (17, 16, 16),
  83. })
  84. text_image = self.add_text_list(text_list, spacing=25, base="nc")
  85. if text_image:
  86. text_image = text_image.paste_img_invert(
  87. top_img=PictureProcessing("RGB", (1200, text_image.height + 100), self.base_bg_color),
  88. base="cc",
  89. value=(0, 0))
  90. detailed_images.append(text_image)
  91. # ====粘贴组合图以及其他图
  92. goods_art_no_list = list(self.data.keys())
  93. # 文字排列
  94. _zuhe_pic_list = []
  95. color_name_list = ["组合", "组合2", "组合3", "组合4", "组合5"]
  96. for color_name in color_name_list:
  97. pp_1 = self.get_overlay_pic_from_dict(
  98. goods_art_no=goods_art_no_list[0],
  99. color_name=color_name,
  100. bg_color=self.base_bg_color,
  101. )
  102. if not pp_1:
  103. continue
  104. pp_1 = pp_1.resize(value=int(pp_bg.width / 1.45))
  105. pp_1 = pp_1.paste_img_invert(
  106. top_img=PictureProcessing("RGB", (pp_bg.width, pp_1.height + 30), self.base_bg_color),
  107. base="cc")
  108. _zuhe_pic_list.append(pp_1)
  109. _zuhe_pic_list.append(PictureProcessing("RGB", (pp_bg.width, 150), self.base_bg_color))
  110. if not _zuhe_pic_list:
  111. pp_1 = self.get_overlay_pic_from_dict(
  112. goods_art_no=goods_art_no_list[0],
  113. color_name="俯视",
  114. bg_color=self.base_bg_color,
  115. )
  116. pp_1 = pp_1.resize(value=int(pp_bg.width / 1.45))
  117. pp_1 = pp_1.paste_img_invert(
  118. top_img=PictureProcessing("RGB", (pp_bg.width, pp_1.height + 30), self.base_bg_color),
  119. base="cc")
  120. _zuhe_pic_list.append(pp_1)
  121. detailed_images.extend(_zuhe_pic_list)
  122. detailed_images.append(PictureProcessing(r"{}\t (3).png".format(self.root)))
  123. return PictureProcessing(im=self.add_pic(detailed_images))
  124. # 商品信息
  125. def deal_pic_2(self):
  126. """
  127. 细节解析
  128. """
  129. # 文字排列
  130. detailed_images = []
  131. pp_bg = PictureProcessing(r"{}\t (4).png".format(self.root))
  132. detailed_images.append(pp_bg)
  133. detailed_images.append(PictureProcessing("RGB", (pp_bg.width, 120), self.base_bg_color))
  134. goods_art_no_list = list(self.data.keys())
  135. pp_bg_2 = PictureProcessing(r"{}\t (26) .png".format(self.root))
  136. pp_1 = self.get_overlay_pic_from_dict(
  137. goods_art_no=goods_art_no_list[0],
  138. color_name="侧视",
  139. bg_color=self.base_bg_color,
  140. )
  141. pp_1 = pp_1.resize(value=700)
  142. pp_bg_2 = pp_bg_2.paste_img(top_img=pp_1,
  143. base="sc",
  144. value=(0, 90)
  145. )
  146. # 粘贴文字
  147. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 26)
  148. text_key_list = ["鞋面特点", "跟高", "鞋底特点"]
  149. for text_key in text_key_list:
  150. text = self.get_text_value(text_key)
  151. if not text:
  152. continue
  153. if text_key == "鞋面特点":
  154. value = (131, 380)
  155. if text_key == "跟高":
  156. value = (935, 187)
  157. text = "跟高:{}".format(text)
  158. if text_key == "鞋底特点":
  159. value = (831, 40)
  160. text_image = pp_bg_2.get_text_image_advanced(
  161. value=(0, 0),
  162. font=font,
  163. text=text,
  164. spacing=5,
  165. fill=(15, 15, 15),
  166. return_mode="min_image",
  167. )
  168. if text_image:
  169. pp_bg_2 = pp_bg_2.paste_img(top_img=text_image,
  170. base="sw",
  171. value=value
  172. )
  173. # 剪裁多余的
  174. if pp_bg_2.height > pp_1.height + 90:
  175. pp_bg_2 = pp_bg_2.crop(bbox=(0, pp_bg_2.height - 90 - pp_1.height, pp_bg_2.width, pp_bg_2.height))
  176. detailed_images.append(pp_bg_2)
  177. font_bg = PictureProcessing(r"{}\t (6).png".format(self.root))
  178. # 商品信息文字
  179. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 35)
  180. text_list = [
  181. ("{}".format(self.get_text_value("品牌")), (294 + 20, 49)),
  182. ("{}".format(self.get_text_value("临时款号")), (854 + 20, 49)),
  183. ("{}".format(self.get_text_value("鞋面材质")), (294 + 20, 138)),
  184. ("{}".format(self.get_text_value("内里材质")), (854 + 20, 138)),
  185. ("{}".format(self.get_text_value("鞋垫材质")), (294 + 20, 230)),
  186. ("{}".format(self.get_text_value("鞋底材质")), (854 + 20, 230)),
  187. ]
  188. for text_item in text_list:
  189. if text_item[0]:
  190. font_bg = font_bg.get_text_image_advanced(
  191. value=text_item[1],
  192. font=font,
  193. text=text_item[0],
  194. align="center",
  195. anchor=None,
  196. spacing=5,
  197. fill=(30, 30, 30),
  198. return_mode="image",
  199. margins=(0, 0, 0, 0)
  200. )
  201. # 其他图片
  202. detailed_images.append(font_bg)
  203. return PictureProcessing(im=self.add_pic(detailed_images))
  204. def deal_pic_3(self):
  205. """ 各个颜色细节展示 """
  206. detailed_images = []
  207. # 添加分割线
  208. detailed_images.append(PictureProcessing("RGB", (1200, 100), (255, 255, 255)))
  209. # 放各个颜色图
  210. pp_list_1 = []
  211. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 25)
  212. goods_art_no_list = list(self.data.keys())
  213. for index, goods_art_no in enumerate(goods_art_no_list):
  214. pp_jpg = self.get_overlay_pic_from_dict(
  215. goods_art_no=goods_art_no,
  216. color_name="侧视",
  217. bg_color=self.base_bg_color,
  218. )
  219. if pp_jpg is None:
  220. continue
  221. pp_jpg = pp_jpg.resize(value=480)
  222. color_name = self.goods_no_value["货号资料"][index]["颜色名称"]
  223. text_bg = PictureProcessing("RGB", (480, 50), (255, 255, 255))
  224. text_bg = text_bg.get_text_image_advanced(
  225. value=(480 / 2, 10),
  226. font=font,
  227. text="{}".format(color_name),
  228. align="center",
  229. anchor="mm",
  230. spacing=5,
  231. fill=(55, 55, 55),
  232. return_mode="image",
  233. margins=(0, 0, 0, 0)
  234. )
  235. # text_bg.show()
  236. _bg = PictureProcessing("RGB", (440, pp_jpg.height + text_bg.height), (255, 255, 255))
  237. _bg = _bg.paste_img(top_img=pp_jpg)
  238. _bg = _bg.paste_img(top_img=text_bg, value=(0, pp_jpg.height))
  239. pp_list_1.append(_bg)
  240. rows = 2
  241. shoes_bg = PictureProcessing().horizontal_distribution(
  242. pp_list=pp_list_1,
  243. bg_width=1200,
  244. margins=(0, 0, 40, 40),
  245. line_spacing=60,
  246. number_per_row=rows,
  247. )
  248. detailed_images.append(shoes_bg)
  249. # 添加分割线
  250. detailed_images.append(PictureProcessing("RGB", (1200, 200), (255, 255, 255)))
  251. return PictureProcessing(im=self.add_pic(detailed_images))
  252. # 细节展示
  253. def deal_pic_4(self):
  254. # =============细节展示================
  255. detail_images = []
  256. top_bg = PictureProcessing(r"{}\t (8).png".format(self.root))
  257. detail_images.append(top_bg)
  258. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Heavy.ttf", 120) # 序号标签
  259. font_2 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 36) # 描述主标题
  260. font_3 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 26) # 描述副标题
  261. # ==========3个图片描述信息=========
  262. goods_art_no_list = list(self.data.keys())
  263. color_name_list = ["俯视", "鞋底", "侧视"]
  264. for index, color_name in enumerate(color_name_list):
  265. # 添加分割线
  266. detail_images.append(PictureProcessing("RGB", (top_bg.width, 20), (255, 255, 255)))
  267. # 添加商品图片
  268. pp_jpg = self.get_overlay_pic_from_dict(
  269. goods_art_no=goods_art_no_list[0],
  270. color_name=color_name,
  271. bg_color=self.base_bg_color
  272. )
  273. if color_name == "俯视":
  274. pp_jpg = pp_jpg.resize(value=1565)
  275. pp_jpg = pp_jpg.paste_img_invert(
  276. top_img=PictureProcessing("RGB", (1200, 1050), (255, 255, 255)),
  277. base="ws",
  278. value=(104, 0))
  279. text_pos_value = (50, 70)
  280. text_pos_base = "nw" # 粘贴文字的位置
  281. text_base = "wn" # 文字方向
  282. if color_name == "鞋底":
  283. pp_jpg = pp_jpg.resize(value=1766)
  284. pp_jpg = pp_jpg.transpose(mode="left_right")
  285. pp_jpg = pp_jpg.paste_img_invert(
  286. top_img=PictureProcessing("RGB", (1200, pp_jpg.height + 150), (255, 255, 255)), base="ec",
  287. value=(275, 0))
  288. text_pos_value = (50, 150)
  289. text_pos_base = "es" # 粘贴文字的位置
  290. text_base = "en"
  291. if color_name == "侧视":
  292. pp_jpg = pp_jpg.resize(value=1681)
  293. pp_jpg = pp_jpg.transpose(mode="left_right")
  294. pp_jpg = pp_jpg.paste_img_invert(
  295. top_img=PictureProcessing("RGB", (1200, 843), (255, 255, 255)), base="ws",
  296. value=(216, 0))
  297. text_pos_value = (50, 150)
  298. text_pos_base = "ws" # 粘贴文字的位置
  299. text_base = "wn"
  300. text_list = []
  301. # 序号
  302. text_list.append({"text": "{}".format(index + 1).zfill(2),
  303. "font": font_1,
  304. "fill": (215, 215, 215),
  305. })
  306. # 主标题
  307. text_list.append({"text": self.get_text_value("提示{}主标题".format(index + 1)),
  308. "font": font_2,
  309. "fill": (55, 55, 55),
  310. })
  311. text_list.append({"text": self.get_text_value("提示{}副标题".format(index + 1)),
  312. "font": font_3,
  313. "fill": (55, 55, 55),
  314. })
  315. text_image = self.add_text_list(text_list, spacing=30, base=text_base)
  316. pp_jpg = pp_jpg.paste_img(
  317. top_img=text_image,
  318. base=text_pos_base,
  319. value=text_pos_value, )
  320. detail_images.append(pp_jpg)
  321. # 添加分割线
  322. detail_images.append(PictureProcessing("RGB", (top_bg.width, 60), (255, 255, 255), ))
  323. # 添加分割线
  324. detail_images.append(PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255), ))
  325. image = PictureProcessing(im=self.add_pic(detail_images))
  326. return image
  327. # 添加尺码表
  328. def deal_pic_5(self):
  329. image_path = r"{}\t (10).png".format(self.root)
  330. return PictureProcessing(image_path)
  331. # 静物展示
  332. def deal_pic_6(self):
  333. bg_color = (255, 255, 255)
  334. detailed_images = []
  335. goods_art_no_list = list(self.data.keys())
  336. top_bg = PictureProcessing(r"{}\t (11).png".format(self.root))
  337. detailed_images.append(top_bg)
  338. pp_list_1 = []
  339. color_name_show_list = []
  340. color_name_show_list.append(["组合", "内里", "侧视", "鞋底", "俯视"])
  341. color_name_show_list.append(["组合2", "内里", "侧视", "鞋底", "俯视"])
  342. color_name_show_list.append(["组合3", "内里", "侧视", "鞋底", "俯视"])
  343. color_name_show_list.append(["内里", "侧视", "鞋底", "俯视"])
  344. color_name_text_show_dict = {
  345. "组合": "合并展示 / MERGE",
  346. "组合2": "合并展示 / MERGE",
  347. "组合3": "合并展示 / MERGE",
  348. "内里": "内里展示 / INSIDER",
  349. "鞋底": "鞋底展示 / SOLE",
  350. "侧视": "后跟展示 / HEELPIECE",
  351. "俯视": "45°展示 / 45°",
  352. }
  353. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)
  354. n = 0
  355. pp_1_height = 100
  356. for index, goods_art_no in enumerate(goods_art_no_list):
  357. try:
  358. color_name = color_name_show_list[index]
  359. except:
  360. color_name = color_name_show_list[-1]
  361. for name in color_name:
  362. pp_1 = self.get_overlay_pic_from_dict(
  363. goods_art_no=goods_art_no,
  364. color_name=name,
  365. bg_color=self.base_bg_color
  366. )
  367. if pp_1:
  368. n += 1
  369. if name != "后跟":
  370. pp_1 = pp_1.resize(value=int(top_bg.width / 1.4))
  371. else:
  372. pp_1 = pp_1.resize(value=int(top_bg.width / 3))
  373. if n == 1:
  374. pp_1_height = pp_1.height
  375. _height = pp_1.height if pp_1.height > pp_1_height else pp_1_height
  376. _height = pp_1.height + 50
  377. # pp_1 = pp_1.paste_img_invert(base="sc",
  378. # top_img=PictureProcessing("RGB",
  379. # (int(top_bg.width / 1.4), _height + 10,),
  380. # self.base_bg_color))
  381. pp_1 = pp_1.paste_img_invert(base="sc",
  382. value=(0, 50),
  383. top_img=PictureProcessing("RGB",
  384. (top_bg.width, _height + 100,),
  385. (255, 255, 255)))
  386. pp_list_1.append(pp_1)
  387. font_bg = PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255))
  388. font_bg = font_bg.get_text_image_advanced(
  389. value=(600, 20),
  390. font=font,
  391. text=color_name_text_show_dict[name],
  392. align="center",
  393. anchor="mm",
  394. spacing=5,
  395. fill=(30, 30, 30),
  396. return_mode="image",
  397. margins=(0, 0, 0, 0)
  398. )
  399. pp_list_1.append(font_bg)
  400. if pp_list_1:
  401. pp_list_1.append(PictureProcessing("RGB", (top_bg.width, 100), bg_color))
  402. detailed_images.extend(pp_list_1)
  403. # return PictureProcessing(im=self.add_pic(detailed_images))
  404. return self.pp_pic_subsection(PictureProcessing(im=self.add_pic(detailed_images)))
  405. # 添加备注图
  406. def deal_pic_7(self):
  407. image_path = r"{}\t (12).png".format(self.root)
  408. return PictureProcessing(image_path)