detail_huilima6.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. """
  2. 步骤:
  3. 1、整理需要处理的款号图-输出款号图文件夹
  4. 2、整理所有相关的图片作为素材图
  5. 3、按要求进行拼接
  6. """
  7. import os
  8. import time
  9. from PIL import Image, ImageFont
  10. import sys
  11. import settings
  12. import requests
  13. import json, base64, io
  14. # from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
  15. # from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
  16. is_test_plugins = False
  17. try:
  18. is_test_plugins = settings.is_test_plugins
  19. except:
  20. is_test_plugins = False
  21. if is_test_plugins:
  22. from custom_plugins.plugins_mode.detail_generate_base import DetailBase
  23. from custom_plugins.plugins_mode.pic_deal import PictureProcessing
  24. else:
  25. from plugins_mode.detail_generate_base import DetailBase
  26. from plugins_mode.pic_deal import PictureProcessing
  27. plugins_name = "详情模板"
  28. company_name_list = ["全部"]
  29. template_name = "huilima-6"
  30. def download_image_pil(url):
  31. yzmdata = requests.get(url)
  32. tempIm = io.BytesIO(yzmdata.content)
  33. im = Image.open(tempIm)
  34. return im
  35. # 乐福鞋 详情页
  36. class DetailPicGet(DetailBase):
  37. need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
  38. root = r"{}\resources\detail_temp\huilima\4".format(os.getcwd())
  39. def __init__(
  40. self,
  41. goods_no,
  42. goods_no_value: dict,
  43. out_put_dir,
  44. windows=None,
  45. test=False,
  46. excel_data=None,
  47. assigned_page_list=None,
  48. output_queue=None,
  49. **kwargs,
  50. ):
  51. super().__init__(
  52. goods_no,
  53. goods_no_value,
  54. out_put_dir,
  55. windows=windows,
  56. excel_data=excel_data,
  57. assigned_page_list=assigned_page_list,
  58. output_queue=output_queue,
  59. )
  60. self.template_name = template_name
  61. self.root = r"{}\resources\detail_temp\huilima\4".format(os.getcwd())
  62. print("run huilima-1 ")
  63. self.base_bg_color = (255, 255, 255)
  64. self.deal_pic_func_list = [
  65. self.deal_pic_1,
  66. self.deal_pic_2,
  67. self.deal_pic_3,
  68. self.deal_pic_7,
  69. self.deal_pic_4,
  70. self.deal_pic_5,
  71. self.deal_pic_6,
  72. ]
  73. if test:
  74. self.run_test()
  75. else:
  76. self.run_all()
  77. def run_test(self):
  78. detailed_images = []
  79. detailed_images.append(self.deal_pic_1())
  80. detailed_images.append(self.deal_pic_2())
  81. detailed_images.append(self.deal_pic_3())
  82. detailed_images.append(self.deal_pic_7())
  83. detailed_images.append(self.deal_pic_4())
  84. detailed_images.append(self.deal_pic_5())
  85. detailed_images.append(self.deal_pic_6())
  86. img = self.add_pic(detailed_images)
  87. if img:
  88. self.create_folder(r"{}/{}".format(self.out_put_dir, template_name))
  89. img.save(
  90. r"{}/{}/{}.jpg".format(
  91. self.out_put_dir, template_name, self.goods_no, format="JPEG"
  92. )
  93. )
  94. img.show()
  95. #
  96. def deal_pic_1(self):
  97. detailed_images = []
  98. pp_bg_1 = PictureProcessing(r"{}\1.jpg".format(self.root))
  99. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 120)
  100. text_list = [
  101. {"text": "标题", "pos": (89, 224)},
  102. {"text": "副标题", "pos": (89, 345)},
  103. ]
  104. for text_data in text_list:
  105. text = self.get_text_value(text_data["text"])
  106. if text:
  107. _pp = PictureProcessing().get_text_image_advanced(
  108. font=font_1,
  109. text=text,
  110. fill=(0, 0, 0),
  111. return_mode="min_image",
  112. )
  113. pp_bg_1 = pp_bg_1.paste_img(
  114. top_img=_pp, value=text_data["pos"], base="nw"
  115. )
  116. detailed_images.append(pp_bg_1)
  117. # 粘贴组合图
  118. goods_art_no_list = list(self.data.keys())
  119. pp_jpg, pp_png = self.image_one_pic(
  120. return_orign=True,
  121. goods_art_no=goods_art_no_list[0],
  122. name="组合",
  123. )
  124. if not pp_jpg:
  125. pp_jpg, pp_png = self.image_one_pic(
  126. return_orign=True,
  127. goods_art_no=goods_art_no_list[0],
  128. name="俯视",
  129. )
  130. pp_jpg: PictureProcessing
  131. pp_png: PictureProcessing
  132. pp_jpg = pp_jpg.resize(value=1405)
  133. pp_png = pp_png.resize(value=1405)
  134. pp_bg_2 = PictureProcessing(r"{}\2.jpg".format(self.root))
  135. pp_bg_2 = pp_bg_2.to_overlay_pic_advance(
  136. top_img=pp_jpg, top_png_img=pp_png, base="cc"
  137. )
  138. detailed_images.append(pp_bg_2)
  139. return PictureProcessing(im=self.add_pic(detailed_images))
  140. # 展示基础信息
  141. def deal_pic_2(self):
  142. detailed_images = []
  143. detailed_images.append(PictureProcessing(r"{}\3.jpg".format(self.root)))
  144. # 粘贴侧视图
  145. goods_art_no_list = list(self.data.keys())
  146. pp_jpg = self.get_overlay_pic_from_dict(
  147. goods_art_no=goods_art_no_list[0],
  148. color_name="侧视",
  149. bg_color=self.base_bg_color,
  150. )
  151. pp_jpg = pp_jpg.resize(value=1098)
  152. pp_jpg = pp_jpg.paste_img_invert(
  153. top_img=PictureProcessing(
  154. "RGB", (1600, pp_jpg.height + 100), self.base_bg_color
  155. ),
  156. base="cc",
  157. )
  158. detailed_images.append(pp_jpg)
  159. # 粘贴基础信息
  160. pp_bg_2 = PictureProcessing(r"{}\5.jpg".format(self.root))
  161. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Regular.ttf", 62)
  162. text_list = [
  163. {"text": "鞋面材质", "pos": (767, 176)},
  164. {"text": "内里材质", "pos": (1213, 176)},
  165. {"text": "鞋底材质", "pos": (767, 345)},
  166. {"text": "鞋垫材质", "pos": (1213, 345)},
  167. {"text": "跟高", "pos": (767, 616)},
  168. {"text": "前底厚度", "pos": (1196, 616)},
  169. ]
  170. for text_data in text_list:
  171. text = self.get_text_value(text_data["text"])
  172. if text:
  173. _pp = PictureProcessing().get_text_image_advanced(
  174. font=font_1,
  175. text=text,
  176. fill=(0, 0, 0),
  177. return_mode="min_image",
  178. )
  179. pp_bg_2 = pp_bg_2.paste_img(
  180. top_img=_pp, value=text_data["pos"], base="nw"
  181. )
  182. detailed_images.append(pp_bg_2)
  183. return PictureProcessing(im=self.add_pic(detailed_images))
  184. # 颜色展示
  185. def deal_pic_3(self):
  186. detailed_images = []
  187. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 34) # 颜色名称
  188. color_pic_list_1 = []
  189. for goods_art_no_dict in self.goods_no_value["货号资料"]:
  190. color_name = goods_art_no_dict["颜色名称"]
  191. goods_art_no = goods_art_no_dict["货号"]
  192. pp_jpg = self.get_overlay_pic_from_dict(
  193. goods_art_no=goods_art_no,
  194. color_name="侧视",
  195. bg_color=self.base_bg_color,
  196. )
  197. pp_jpg = pp_jpg.resize(value=390)
  198. pp_jpg = pp_jpg.paste_img_invert(
  199. top_img=PictureProcessing(
  200. "RGBA", (pp_jpg.width, pp_jpg.height + 70), (255, 255, 255, 0)
  201. ),
  202. base="nw",
  203. )
  204. text_image = PictureProcessing().get_text_image_advanced(
  205. font=font_1,
  206. text="{} / COLOR".format(color_name),
  207. fill=(0, 0, 0),
  208. return_mode="min_image",
  209. )
  210. pp_jpg = pp_jpg.paste_img(top_img=text_image, base="sc", value=(0, 10))
  211. color_pic_list_1.append(pp_jpg)
  212. # 颜色列表进行等分展示
  213. all_color_pp = PictureProcessing().horizontal_distribution(
  214. color_pic_list_1, bg_width=1114, line_spacing=10, number_per_row=2
  215. )
  216. all_color_pp = all_color_pp.paste_img_invert(
  217. top_img=PictureProcessing(
  218. "RGB", (1600, all_color_pp.height + 100), self.base_bg_color
  219. ),
  220. base="cc",
  221. value=(0, 0),
  222. )
  223. detailed_images.append(all_color_pp)
  224. return PictureProcessing(im=self.add_pic(detailed_images))
  225. def deal_pic_7(self):
  226. detailed_images = []
  227. temp_bg = PictureProcessing(r"{}\8.jpg".format(self.root))
  228. # temp_bg.width
  229. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Bold.ttf", 68) # 颜色名称
  230. goods_art_no_list = list(self.data.keys())
  231. pp_jpg = self.get_overlay_pic_from_dict(
  232. goods_art_no=goods_art_no_list[0],
  233. color_name="俯视",
  234. bg_color=self.base_bg_color,
  235. )
  236. # tips = "一只鞋放置在向日葵花园的草地上,鞋子后面有一块石头,石头上还有一只小猫,鞋得前面还有一条小溪,这就要求鞋子的角度、材质、纹理保持不变,背景要求富有创意且有真实感"
  237. tips = "The shoes are placed on a neutral beige tone background.The sun shines from one side, creating a soft shadow effect, and the overall style is fresh and natural, suitable for summer wear. Keep the texture and material of this shoe intact"
  238. imageGenerateResult = self.getAiGenerateScenes(image_path=pp_jpg.im, tips=tips)
  239. if imageGenerateResult == None:
  240. return PictureProcessing(im=self.add_pic(detailed_images))
  241. imageGenerateResult = imageGenerateResult.resize(value=temp_bg.width)
  242. imageGenerateResult = imageGenerateResult.radius(
  243. value=30, circular_pos=(1, 1, 1, 1)
  244. )
  245. real_bg = PictureProcessing("RGB", (temp_bg.width, 500), self.base_bg_color)
  246. text_image = PictureProcessing().get_text_image_advanced(
  247. font=font_1,
  248. text="场景展示",
  249. fill=(0, 0, 0),
  250. return_mode="min_image",
  251. )
  252. real_bg = real_bg.paste_img(top_img=text_image, base="cc", value=(0, 100))
  253. detailed_images.append(real_bg)
  254. detailed_images.append(imageGenerateResult)
  255. tips = "The image showcases a half-body shot of a model dressed in a gray dress and white ankle socks. The model is taking a step forward, with one foot slightly ahead. The shoes are precisely fitted to the model's feet, ensuring they are neither too large nor too small, with a subtly elevated heel. The background is a simple neutral beige, creating a clean and distraction-free visual effect. Throughout the process, it is essential to preserve the original texture and style of the shoes, including their unique design features and material feel, making the shoes appear realistic and natural as if truly worn by the model."
  256. imageGenerateResult = self.getAiGenerateScenes(image_path=pp_jpg.im, tips=tips)
  257. if imageGenerateResult == None:
  258. detailed_images.append(
  259. PictureProcessing("RGB", (temp_bg.width, 300), self.base_bg_color)
  260. )
  261. return PictureProcessing(im=self.add_pic(detailed_images))
  262. imageGenerateResult = imageGenerateResult.resize(value=temp_bg.width)
  263. imageGenerateResult = imageGenerateResult.radius(
  264. value=30, circular_pos=(1, 1, 1, 1)
  265. )
  266. detailed_images.append(imageGenerateResult)
  267. detailed_images.append(
  268. PictureProcessing("RGB", (temp_bg.width, 300), self.base_bg_color)
  269. )
  270. return PictureProcessing(im=self.add_pic(detailed_images))
  271. def getAiGenerateScenes(self, image_path: Image, tips: str = ""):
  272. byte_io = io.BytesIO()
  273. # 将图片保存到BytesIO对象中,格式与原图相同
  274. image_path.save(byte_io, format="JPEG")
  275. # 获取BytesIO对象中的二进制数据
  276. image_data = byte_io.getvalue()
  277. # 使用base64编码将二进制数据转换成Base64字符串
  278. encoded_string = base64.b64encode(image_data).decode("utf-8")
  279. url = "https://api.bfl.ai/v1/flux-kontext-max"
  280. headers = {
  281. "x-key": "70f91c34-e494-4dc2-a7d2-635295136d2d",
  282. "Content-Type": "application/json",
  283. }
  284. payload = json.dumps(
  285. {
  286. "prompt": tips,
  287. "input_image": f"data:image/png;base64,{encoded_string}",
  288. "output_format": "jpeg",
  289. "aspect_ratio": "9:12",
  290. "safety_tolerance": 2,
  291. }
  292. )
  293. try:
  294. response = requests.request("POST", url, headers=headers, data=payload)
  295. response.raise_for_status()
  296. response_json = response.json()
  297. polling_url = response_json.get("polling_url")
  298. if polling_url == None:
  299. return None
  300. request_times = 30
  301. result_image = None
  302. i = 0
  303. while i < request_times:
  304. response_get = requests.request("GET", polling_url)
  305. response_get.raise_for_status()
  306. response_get_json = response_get.json()
  307. print("获取图片中...", i + 1, "次")
  308. print("json数据...", response_get_json)
  309. status = response_get_json.get("status")
  310. if status != "Ready":
  311. time.sleep(1)
  312. i += 1
  313. continue
  314. result_image = response_get_json.get("result").get("sample")
  315. if result_image != None:
  316. image_obj = download_image_pil(result_image)
  317. print("图片获取成功...")
  318. return PictureProcessing(im=image_obj)
  319. time.sleep(1)
  320. i += 1
  321. except:
  322. return None
  323. # 展示细节图
  324. def deal_pic_4(self):
  325. detailed_images = []
  326. detailed_images.append(PictureProcessing(r"{}\8.jpg".format(self.root)))
  327. goods_art_no_list = list(self.data.keys())
  328. goods_art_no = goods_art_no_list[0]
  329. font_1 = ImageFont.truetype(r"resources\ttf\puhui\Medium.ttf", 85) # 颜色名称
  330. view_data_list = ["俯视", "俯视", "侧视"]
  331. text_list = ["提示1主标题", "提示2主标题", "提示3主标题"]
  332. bg_name_list = ["10.jpg", "12.jpg", "14.jpg"]
  333. for index, view_name in enumerate(view_data_list):
  334. pp_jpg = self.get_overlay_pic_from_dict(
  335. goods_art_no=goods_art_no,
  336. color_name=view_name,
  337. bg_color=(246, 246, 246),
  338. )
  339. if index == 0:
  340. resize_value = 3200
  341. paste_base = "cc"
  342. paste_value = (0, 200)
  343. if index == 1:
  344. resize_value = 3200
  345. paste_base = "ne"
  346. paste_value = (-100, 0)
  347. if index == 2:
  348. pp_jpg = pp_jpg.transpose(mode="left_right")
  349. resize_value = 3513
  350. paste_base = "sw"
  351. paste_value = (0, 100)
  352. pp_jpg = pp_jpg.resize(value=resize_value)
  353. pp_jpg = pp_jpg.paste_img_invert(
  354. top_img=PictureProcessing("RGB", (1500, 1500), (246, 246, 246)),
  355. base=paste_base,
  356. value=paste_value,
  357. )
  358. pp_jpg = pp_jpg.radius(value=100)
  359. pp_jpg = pp_jpg.paste_img_invert(
  360. top_img=PictureProcessing("RGB", (1600, 1600), (255, 255, 255)),
  361. base="cc",
  362. )
  363. detailed_images.append(pp_jpg)
  364. # 添加文字
  365. text = self.get_text_value(text_list[index])
  366. if text:
  367. pp_text_bg = PictureProcessing(
  368. r"{}\{}".format(self.root, bg_name_list[index])
  369. )
  370. _pp = PictureProcessing().get_text_image_advanced(
  371. font=font_1,
  372. text=text,
  373. fill=(0, 0, 0),
  374. return_mode="min_image",
  375. )
  376. pp_text_bg = pp_text_bg.paste_img(top_img=_pp, value=(73, 0), base="wc")
  377. detailed_images.append(pp_text_bg)
  378. return PictureProcessing(im=self.add_pic(detailed_images))
  379. # 场景图--产品展示
  380. def deal_pic_5(self):
  381. detailed_images = []
  382. detailed_images.append(PictureProcessing(r"{}\15.jpg".format(self.root)))
  383. goods_art_no_list = list(self.data.keys())
  384. goods_art_no = goods_art_no_list[0]
  385. _flag = True
  386. pp_jpg = self.get_overlay_pic_from_dict(
  387. goods_art_no=goods_art_no, color_name="组合1", bg_color=(255, 255, 255)
  388. )
  389. if pp_jpg:
  390. pp_jpg = pp_jpg.paste_img_invert(
  391. top_img=PictureProcessing(
  392. "RGB", (1600, pp_jpg.height + 200), (255, 255, 255)
  393. ),
  394. base="cc",
  395. )
  396. detailed_images.append(pp_jpg)
  397. else:
  398. _flag = False
  399. pp_jpg = self.get_overlay_pic_from_dict(
  400. goods_art_no=goods_art_no, color_name="组合2", bg_color=(255, 255, 255)
  401. )
  402. if pp_jpg:
  403. pp_jpg = pp_jpg.paste_img_invert(
  404. top_img=PictureProcessing(
  405. "RGB", (1600, pp_jpg.height + 200), (255, 255, 255)
  406. ),
  407. base="cc",
  408. )
  409. detailed_images.append(pp_jpg)
  410. else:
  411. _flag = False
  412. if not _flag:
  413. detailed_images = []
  414. detailed_images.append(PictureProcessing(r"{}\15.jpg".format(self.root)))
  415. for goods_art_no_dict in self.goods_no_value["货号资料"]:
  416. color_name = goods_art_no_dict["颜色名称"]
  417. goods_art_no = goods_art_no_dict["货号"]
  418. pp_jpg = self.get_overlay_pic_from_dict(
  419. goods_art_no=goods_art_no,
  420. color_name="侧视",
  421. bg_color=self.base_bg_color,
  422. )
  423. pp_jpg = pp_jpg.resize(value=1404)
  424. pp_jpg = pp_jpg.paste_img_invert(
  425. top_img=PictureProcessing(
  426. "RGB", (1600, pp_jpg.height + 200), (255, 255, 255)
  427. ),
  428. base="cc",
  429. )
  430. detailed_images.append(pp_jpg)
  431. return PictureProcessing(im=self.add_pic(detailed_images))
  432. # 添加注意事项
  433. def deal_pic_6(self):
  434. detailed_images = []
  435. detailed_images.append(PictureProcessing(r"{}\20.jpg".format(self.root)))
  436. detailed_images.append(PictureProcessing(r"{}\21.jpg".format(self.root)))
  437. return PictureProcessing(im=self.add_pic(detailed_images))