ceshi.py 15 KB

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