detail_xiaosushuoxie7.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. try:
  13. is_test_plugins = settings.is_test_plugins
  14. except:
  15. is_test_plugins = False
  16. if is_test_plugins:
  17. from custom_plugins.plugins_mode.detail_generate_base import DetailBase
  18. from custom_plugins.plugins_mode.pic_deal import PictureProcessing
  19. else:
  20. from plugins_mode.detail_generate_base import DetailBase
  21. from plugins_mode.pic_deal import PictureProcessing
  22. from PIL import Image, ImageDraw
  23. plugins_name = "详情模板"
  24. company_name_list = ["小苏"]
  25. template_name = "xiaosushuoxie-7"
  26. class DetailPicGet(DetailBase):
  27. need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
  28. root = r"{}\resources\detail_temp\xiaosushuoxie\7".format(os.getcwd())
  29. def __init__(self, goods_no, goods_no_value: dict, out_put_dir, windows=None, test=False,excel_data=None,assigned_page_list=None):
  30. super().__init__(goods_no, goods_no_value, out_put_dir, windows=windows,excel_data=excel_data,assigned_page_list=assigned_page_list)
  31. self.root = r"{}\resources\detail_temp\xiaosushuoxie\7".format(os.getcwd())
  32. self.template_name = template_name
  33. self.base_bg_color = (236, 226, 211)
  34. self.white_bg_color = (255, 255, 255)
  35. self.deal_pic_func_list = [
  36. self.deal_pic_1,
  37. # self.deal_pic_2,
  38. # self.deal_pic_3,
  39. # self.deal_pic_4,
  40. # self.deal_pic_5,
  41. ]
  42. if test:
  43. self.run_test()
  44. else:
  45. self.run_all()
  46. def run_test(self):
  47. detailed_images = []
  48. detailed_images.append(self.deal_pic_1())
  49. # detailed_images.append(self.deal_pic_2())
  50. # detailed_images.append(self.deal_pic_3())
  51. # detailed_images.append(self.deal_pic_4())
  52. # detailed_images.append(self.deal_pic_5())
  53. img = self.add_pic(detailed_images)
  54. img.save(r"{}/{}.jpg".format(self.out_put_dir,self.goods_no, format="JPEG"))
  55. def deal_pic_1(self):
  56. """ 制作主图 """
  57. detailed_images = []
  58. pp_bg = PictureProcessing(r"{}\template_1.jpg".format(self.root))
  59. # -------粘贴文字-------
  60. mainTitle = self.get_text_value("主标题")
  61. subTitle = self.get_text_value("副标题")
  62. signTitle = self.get_text_value("签名")
  63. fontMain = ImageFont.truetype(r"resources\ttf\DOUYINSANSBOLD.ttf", 240)
  64. fontSub = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 40)
  65. fontSign = ImageFont.truetype(r"resources\ttf\puhui\en\Bold.otf", 40)
  66. main_text_bg = PictureProcessing("RGBA", (pp_bg.width, 500), (255, 255, 255,0))
  67. main_text_bg = main_text_bg.get_text_image_advanced(
  68. value=(0, 0),
  69. font=fontMain,
  70. text=mainTitle,
  71. align="center",
  72. fill=self.white_bg_color,
  73. spacing=5,
  74. return_mode="min_image",
  75. )
  76. # 副标题
  77. sub_text_bg = PictureProcessing("RGBA", (pp_bg.width, 500), (255, 255, 255,0))
  78. sub_text_bg = sub_text_bg.get_text_image_advanced(
  79. value=(0, 0),
  80. font=fontSub,
  81. text=subTitle,
  82. align="center",
  83. fill=self.white_bg_color,
  84. spacing=5,
  85. return_mode="min_image",
  86. )
  87. # 签名
  88. sign_text_bg = PictureProcessing("RGBA", (pp_bg.width, 500), (255, 255, 255,0))
  89. sign_text_bg = sign_text_bg.get_text_image_advanced(
  90. value=(0, 0),
  91. font=fontSign,
  92. text=signTitle,
  93. align="center",
  94. fill=self.white_bg_color,
  95. spacing=5,
  96. return_mode="min_image",
  97. )
  98. pp_bg = pp_bg.paste_img(top_img=main_text_bg,value=(0,208),base='nc')
  99. pp_bg = pp_bg.paste_img(top_img=sub_text_bg,value=(0,470),base='nc')
  100. pp_bg = pp_bg.paste_img(top_img=sign_text_bg,value=(0,57),base='nc')
  101. detailed_images.append(pp_bg)
  102. return PictureProcessing(im=self.add_pic(detailed_images))
  103. def deal_pic_2(self):
  104. """
  105. 细节解析
  106. """
  107. # 文字排列
  108. detailed_images = []
  109. pp_bg = PictureProcessing(
  110. r"{}\template_2.png".format(
  111. self.root
  112. )
  113. )
  114. detailed_images.append(pp_bg)
  115. goods_art_no_list = list(self.data.keys())
  116. pp_1 = self.get_overlay_pic_from_dict(
  117. goods_art_no=goods_art_no_list[0],
  118. color_name="侧视",
  119. bg_color=self.base_bg_color,
  120. )
  121. pp_1= pp_1.resize(value=pp_bg.width / 1.4)
  122. pp_1= pp_1.paste_img_invert(top_img=PictureProcessing("RGB", (pp_bg.width, pp_1.height + 40), self.base_bg_color),
  123. base="cc"
  124. )
  125. detailed_images.append(pp_1)
  126. pp_bg_bottom = PictureProcessing(r"{}\template_2_1.png".format(self.root))
  127. detailed_images.append(pp_bg_bottom)
  128. return PictureProcessing(im=self.add_pic(detailed_images))
  129. def deal_pic_3(self):
  130. """ 各个颜色细节展示 """
  131. detailed_images = []
  132. pp_bg = PictureProcessing(
  133. r"{}\template_3.png".format(
  134. self.root
  135. )
  136. )
  137. detailed_images.append(pp_bg)
  138. # ----------主图放置
  139. y = 120
  140. pp_list_1 = []
  141. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 25)
  142. goods_art_no_list = list(self.data.keys())
  143. all_color_name = []
  144. for index, goods_art_no in enumerate(goods_art_no_list):
  145. pp_jpg = self.get_overlay_pic_from_dict(
  146. goods_art_no=goods_art_no,
  147. color_name="侧视",
  148. bg_color=self.base_bg_color,
  149. )
  150. if pp_jpg is None:
  151. continue
  152. pp_jpg = pp_jpg.resize(value=440)
  153. color_name = self.goods_no_value["货号资料"][index]["颜色名称"]
  154. all_color_name.append(color_name)
  155. text_bg = PictureProcessing("RGB", (440, 200), (255, 255, 255))
  156. text_bg = text_bg.get_text_image_advanced(
  157. value=(220, 10),
  158. font=font,
  159. text="【{}】".format(color_name),
  160. align="center",
  161. anchor="mm",
  162. spacing=5,
  163. fill=(55, 55, 55),
  164. return_mode="min_image_high",
  165. margins=(10, 5, 0, 0)
  166. )
  167. # text_bg.show()
  168. _bg = PictureProcessing("RGB", (440, pp_jpg.height+text_bg.height), (255, 255, 255))
  169. _bg = _bg.paste_img(top_img=pp_jpg)
  170. _bg = _bg.paste_img(top_img=text_bg,value=(0,pp_jpg.height))
  171. pp_list_1.append(_bg)
  172. rows = 2
  173. shoes_bg = PictureProcessing().horizontal_distribution(
  174. pp_list=pp_list_1,
  175. bg_width=1200,
  176. margins=(0, 0, 40, 40),
  177. line_spacing=60,
  178. number_per_row=rows,
  179. )
  180. detailed_images.append(shoes_bg)
  181. font_bg = PictureProcessing(r"{}\template_3_1_new.png".format(self.root))
  182. font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)
  183. base_x = pp_bg.width - 229
  184. base_y = 205
  185. text_list = [
  186. ("-{}".format("-".join(all_color_name)), (base_x, base_y)),
  187. ("-{}".format(self.get_text_value("鞋面材质")), (base_x, base_y+70*1)),
  188. ("-{}".format(self.get_text_value("内里材质")), (base_x, base_y+70*2)),
  189. ("-{}".format(self.get_text_value("鞋底材质")), (base_x, base_y+70*3)),
  190. ("-{}".format(self.get_text_value("鞋垫材质")), (base_x, base_y+70*4 )),
  191. ("-{}".format(self.get_text_value("跟高")), (base_x, base_y+70*5)),
  192. ("-{}".format(self.get_text_value("尺码")), (base_x, base_y+70*6)),
  193. ]
  194. for text_item in text_list:
  195. position = text_item[1]
  196. text_str = text_item[0]
  197. xPos = position[0]
  198. for char in reversed(text_str):
  199. font_bg.add_text(
  200. mode="pixel",
  201. value=(xPos, position[1]),
  202. font=font,
  203. text=char,
  204. align="right",
  205. anchor="mm",
  206. spacing=10,
  207. fill=(30, 30, 30),
  208. )
  209. if not "\u4e00" <= char <= "\u9fff":
  210. xPos -= 20
  211. else:
  212. xPos -= 35
  213. # 其他图片
  214. detailed_images.append(font_bg)
  215. return PictureProcessing(im=self.add_pic(detailed_images))
  216. def deal_pic_4(self):
  217. # =============设计理念================
  218. detail_images = []
  219. top_bg = PictureProcessing(r"{}\template_4.png".format(self.root))
  220. detail_images.append(top_bg)
  221. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 40)
  222. # 粘贴文字
  223. text = self.get_text_value("设计理念")
  224. if not text:
  225. text = "引领全新裸足脚感,开启自信步履亲肤\n优质鞋面材质赋予全新舒适脚感\n多元场景穿搭,满足你的不同STYLE"
  226. _, text_height = self.get_font_render_size(text, font_1)
  227. text_bg = PictureProcessing(
  228. "RGB",
  229. (
  230. top_bg.width,
  231. text_height + 350,
  232. ),
  233. (236, 227, 212),
  234. )
  235. goods_art_no_list = list(self.data.keys())
  236. # y = 430
  237. x = top_bg.width / 2
  238. text_bg = text_bg.add_text(
  239. mode="pixel",
  240. value=(x, int(text_bg.height / 2) - 60),
  241. font=font_1,
  242. text=text,
  243. align="center",
  244. anchor="mm",
  245. spacing=15,
  246. fill=(97, 97, 98),
  247. )
  248. detail_images.append(text_bg)
  249. # ==========3个提示信息=========
  250. title_font = ImageFont.truetype(r"resources\ttf\puhui\Heavy.ttf", 60)
  251. sub_title_font = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 30)
  252. desc_font = ImageFont.truetype(r"resources\ttf\puhui\Heavy.ttf", 35)
  253. color_name_list = ["俯视", "内里", "侧视"]
  254. # 添加分割线
  255. detail_images.append(PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255)))
  256. for index, color_name in enumerate(color_name_list):
  257. # 添加分割线
  258. detail_images.append(PictureProcessing("RGB", (top_bg.width, 20), (255, 255, 255)))
  259. t_title = self.get_text_value("提示{}主标题".format(index + 1))
  260. if t_title:
  261. t_title_pp = PictureProcessing("RGB", (top_bg.width, 500), (255, 255, 255))
  262. t_title_pp=t_title_pp.get_text_image_advanced(
  263. value=(int(top_bg.width / 2), 0),
  264. font=title_font,
  265. text=t_title,
  266. align="center",
  267. anchor="ma",
  268. spacing=10,
  269. fill=(55, 55, 55),
  270. return_mode="min_image_high",
  271. margins=(10, 10, 0, 0)
  272. )
  273. detail_images.append(t_title_pp)
  274. t_sub_title = self.get_text_value("提示{}副标题".format(index + 1))
  275. if t_sub_title:
  276. t_sub_title_pp = PictureProcessing("RGB", (top_bg.width, 500), (255, 255, 255))
  277. t_sub_title_pp = t_sub_title_pp.get_text_image_advanced(
  278. value=(int(top_bg.width / 2),0),
  279. font=sub_title_font,
  280. text=t_sub_title,
  281. align="center",
  282. anchor="ma",
  283. spacing=10,
  284. fill=(55, 55, 55),
  285. return_mode="min_image_high",
  286. margins=(10, 10, 0, 0)
  287. )
  288. detail_images.append(t_sub_title_pp)
  289. # 添加图
  290. pp_jpg = self.get_overlay_pic_from_dict(
  291. goods_art_no=goods_art_no_list[0],
  292. color_name=color_name,
  293. bg_color=self.base_bg_color
  294. )
  295. t_desc = self.get_text_value("提示{}描述".format(index + 1))
  296. if color_name == "俯视":
  297. pp_jpg = pp_jpg.resize(value=1300)
  298. pp_jpg = pp_jpg.paste_img_invert(
  299. top_img=PictureProcessing("RGB", (749, 1009 if pp_jpg.height>1009 else pp_jpg.height), (236, 227, 212)), base="ws",
  300. value=(-300, -50))
  301. text_pos_value = (50, 50)
  302. align = "left"
  303. anchor = ""
  304. if color_name == "内里":
  305. pp_jpg = pp_jpg.resize(value=2000)
  306. pp_jpg = pp_jpg.paste_img_invert(
  307. top_img=PictureProcessing("RGB", (749, pp_jpg.height + 150), (236, 227, 212)), base="wc",
  308. value=(-230, 0))
  309. text_pos_value = (50, 50)
  310. align = "left"
  311. anchor = ""
  312. if color_name == "侧视":
  313. pp_jpg = pp_jpg.resize(value=1600)
  314. pp_jpg = pp_jpg.paste_img_invert(
  315. top_img=PictureProcessing("RGB", (749, 1009 if pp_jpg.height>1009 else pp_jpg.height), (236, 227, 212)), base="se",
  316. value=(0, -30))
  317. text_pos_value = (pp_jpg.width - 30, pp_jpg.height - 360)
  318. align = "right"
  319. anchor = "rs"
  320. if t_desc:
  321. pp_jpg = pp_jpg.add_text(
  322. mode="pixel",
  323. value=text_pos_value,
  324. font=desc_font,
  325. text=t_desc,
  326. align=align,
  327. anchor=anchor,
  328. spacing=10,
  329. fill=(102, 102, 102),
  330. )
  331. pp_jpg = pp_jpg.paste_img_invert(
  332. top_img=PictureProcessing("RGB", (top_bg.width, pp_jpg.height,), (255, 255, 255), ), base="cc",
  333. value=(0, 0))
  334. detail_images.append(pp_jpg)
  335. # 添加分割线
  336. detail_images.append(PictureProcessing("RGB", (top_bg.width, 60), (255, 255, 255), ))
  337. # 添加分割线
  338. detail_images.append(PictureProcessing("RGB", (top_bg.width, 100), (255, 255, 255), ))
  339. image = PictureProcessing(im=self.add_pic(detail_images))
  340. return image
  341. def deal_pic_5(self):
  342. bg_color = (255, 255, 255)
  343. detailed_images = []
  344. goods_art_no_list = list(self.data.keys())
  345. top_bg = PictureProcessing(
  346. r"{}\template_5.png".format(
  347. self.root
  348. )
  349. )
  350. detailed_images.append(top_bg)
  351. pp_list_1 = []
  352. color_name_1 = ["俯视", "侧视", "内里", "后跟", "鞋底"]
  353. color_name_2 = ["俯视", "鞋底"]
  354. color_name_1 = ["俯视", "侧视", "鞋底"]
  355. color_name_2 = ["俯视", "内里", "后跟"]
  356. n = 0
  357. pp_1_height = 100
  358. for index, goods_art_no in enumerate(goods_art_no_list):
  359. if index in [0, 2, 4, 6, 8]:
  360. color_name = color_name_1
  361. else:
  362. color_name = color_name_2
  363. for name in color_name:
  364. pp_1 = self.get_overlay_pic_from_dict(
  365. goods_art_no=goods_art_no,
  366. color_name=name,
  367. bg_color=self.base_bg_color
  368. )
  369. if pp_1:
  370. n += 1
  371. if name != "后跟":
  372. pp_1 = pp_1.resize(value=int(top_bg.width / 1.4))
  373. else:
  374. pp_1 = pp_1.resize(value=int(top_bg.width / 3))
  375. if n == 1:
  376. pp_1_height = pp_1.height
  377. _height = pp_1.height if pp_1.height > pp_1_height else pp_1_height
  378. pp_1 = pp_1.paste_img_invert(base="cc",
  379. top_img=PictureProcessing("RGB",
  380. (int(top_bg.width / 1.4), _height + 10,),
  381. self.base_bg_color))
  382. pp_1 = pp_1.paste_img_invert(base="cc",
  383. top_img=PictureProcessing("RGB",
  384. (top_bg.width, _height + 100,),
  385. (255, 255, 255)))
  386. pp_list_1.append(pp_1)
  387. if pp_list_1:
  388. pp_list_1.append(PictureProcessing("RGB", (top_bg.width, 100), bg_color))
  389. detailed_images.extend(pp_list_1)
  390. # return PictureProcessing(im=self.add_pic(detailed_images))
  391. return self.pp_pic_subsection(PictureProcessing(im=self.add_pic(detailed_images)))
  392. def get_font_render_size(self, text, font, canvas_size=(2048, 2048)):
  393. canvas = Image.new('RGB', canvas_size)
  394. draw = ImageDraw.Draw(canvas)
  395. draw.text((0, 0), text, font=font, fill=(55, 55, 55))
  396. bbox = canvas.getbbox()
  397. # 宽高
  398. size = (bbox[2] - bbox[0], bbox[3] - bbox[1])
  399. return size