detail_xinjunlue1.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. """
  2. 步骤:
  3. 1、整理需要处理的款号图-输出款号图文件夹
  4. 2、整理所有相关的图片作为素材图
  5. 3、按要求进行拼接
  6. """
  7. import os
  8. from PIL import ImageFont, ImageDraw
  9. import sys
  10. import settings
  11. # from PIL import Image, ImageDraw, ImageFont
  12. # from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
  13. # from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
  14. is_test_plugins = False
  15. try:
  16. is_test_plugins = settings.is_test_plugins
  17. except:
  18. is_test_plugins = False
  19. if is_test_plugins:
  20. from custom_plugins.plugins_mode.detail_generate_base import DetailBase
  21. from custom_plugins.plugins_mode.pic_deal import PictureProcessing
  22. else:
  23. from plugins_mode.detail_generate_base import DetailBase
  24. from plugins_mode.pic_deal import PictureProcessing
  25. plugins_name = "详情模板"
  26. company_name_list = ["全部"]
  27. template_name = "xinjunlue-1"
  28. # 乐福鞋 详情页
  29. class DetailPicGet(DetailBase):
  30. need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
  31. root = r"{}\resources\detail_temp\zhudi\1".format(os.getcwd())
  32. def __init__(
  33. self,
  34. goods_no,
  35. goods_no_value: dict,
  36. out_put_dir,
  37. windows=None,
  38. test=False,
  39. excel_data=None,
  40. assigned_page_list=None,
  41. output_queue=None,
  42. **kwargs
  43. ):
  44. super().__init__(
  45. goods_no,
  46. goods_no_value,
  47. out_put_dir,
  48. windows=windows,
  49. excel_data=excel_data,
  50. assigned_page_list=assigned_page_list,
  51. output_queue=output_queue,
  52. )
  53. self.template_name = template_name
  54. self.root = r"{}\resources\detail_temp\zhudi\1".format(os.getcwd())
  55. print(f"run {template_name} ")
  56. self.base_bg_color = (255, 255, 255)
  57. self.deal_pic_func_list = [
  58. self.deal_pic_1(),
  59. self.deal_pic_2(),
  60. ]
  61. img = self.add_pic(self.deal_pic_func_list)
  62. file_path = self.get_text_value("图片路径")
  63. # file_path = file_path.replace("\\", "/")
  64. parent_folder = os.path.dirname(file_path)
  65. if os.path.exists(parent_folder) == False:
  66. # 确保目标目录存在
  67. os.makedirs(parent_folder, exist_ok=True)
  68. print("file_path是否存在",parent_folder, os.path.exists(parent_folder))
  69. file_extension = file_path.split(".")[-1]
  70. format_mapping = {"jpg": "JPEG", "png": "PNG", "webp": "WEBP", "avif": "AVIF"}
  71. file_format = format_mapping[file_extension]
  72. img.save(file_path, format=file_format)
  73. img.show()
  74. # if test:
  75. # self.run_test()
  76. # else:
  77. # self.run_all()
  78. def run_test(self):
  79. detailed_images = []
  80. detailed_images.append(self.deal_pic_1())
  81. detailed_images.append(self.deal_pic_2())
  82. img = self.add_pic(detailed_images)
  83. if img:
  84. self.create_folder(r"{}/{}".format(self.out_put_dir, template_name))
  85. # print("保存图片成功", img.show())
  86. img.save(
  87. r"{}/{}/{}.jpg".format(
  88. self.out_put_dir, template_name, self.goods_no, format="JPEG"
  89. )
  90. )
  91. img.show()
  92. #
  93. def deal_pic_1(self):
  94. detailed_images = []
  95. base_bg_width = 1200
  96. base_limit_width = base_bg_width / 2
  97. font = ImageFont.truetype("msyh.ttc", 20)
  98. dataList = [
  99. "款号",
  100. "面料",
  101. "内里",
  102. "垫脚",
  103. "大底",
  104. "饰扣",
  105. "客户",
  106. "码数",
  107. "日期",
  108. ]
  109. height_total = 0
  110. text_list = []
  111. line_space = 15
  112. for idx, item in enumerate(dataList):
  113. info = self.get_text_value(item)
  114. if info == None or info == "":
  115. continue
  116. _pp = PictureProcessing()
  117. text = f"{item}: {self.get_text_value(item)}"
  118. duanluo = ""
  119. sum_width = 0
  120. line_count = 0
  121. for char in text:
  122. text_width = font.getlength(char)
  123. sum_width += text_width
  124. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  125. line_count += 1
  126. sum_width = 0
  127. duanluo += '\n'
  128. duanluo += char
  129. _pp = _pp.get_text_image_advanced(
  130. font=font,
  131. text=duanluo,
  132. spacing=10,
  133. fill=(0, 0, 0),
  134. return_mode="min_image_high",
  135. )
  136. height_total += _pp.height + line_space
  137. text_list.append(_pp)
  138. pp_bg_jpg = PictureProcessing(
  139. "RGB", (base_bg_width, height_total), (255, 255, 255)
  140. )
  141. goods_art_no_list = list(self.data.keys())
  142. current_y = 0
  143. for index, text_item in enumerate(text_list):
  144. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(10, current_y))
  145. # 更新 Y 坐标为当前项的底部位置
  146. current_y += text_item.height + line_space
  147. pp_jpg, pp_png = self.image_one_pic(
  148. return_orign=True,
  149. goods_art_no=goods_art_no_list[0],
  150. name="俯视",
  151. )
  152. pp_png = pp_png.resize(value=base_limit_width)
  153. background_size = 0
  154. if pp_png.height > pp_bg_jpg.height:
  155. background_size = pp_png.height+100
  156. else:
  157. background_size = pp_bg_jpg.height + 100
  158. pp_image_jpg = PictureProcessing(
  159. "RGB", (base_bg_width, background_size), (255, 255, 255)
  160. )
  161. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  162. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  163. detailed_images.append(pp_image_jpg)
  164. return PictureProcessing(im=self.add_pic(detailed_images))
  165. def deal_pic_2(self):
  166. detailed_images = []
  167. base_bg_width = 1200
  168. base_limit_width = base_bg_width / 2
  169. font = ImageFont.truetype("arial.ttf", 20)
  170. dataList = [
  171. "JEFFREY CAMPBELL",
  172. "Supplier",
  173. "款号", # Fty No
  174. "Material",
  175. "Lining&Sock",
  176. "码数", # Size
  177. "日期", # Date
  178. "Remark",
  179. "MADE IN CHINA",
  180. ]
  181. height_total = 0
  182. text_list = []
  183. line_space = 15
  184. for idx, item in enumerate(dataList):
  185. info = self.get_text_value(item)
  186. if info == None or info == "":
  187. continue
  188. mapping = {
  189. "JEFFREY CAMPBELL": "",
  190. "Supplier": "Supplier:",
  191. "款号": "Fty No:",
  192. "Material": "Material:",
  193. "Lining&Sock": "Lining&Sock:",
  194. "码数": "Size:",
  195. "日期": "Date:",
  196. "Remark": "Remark:",
  197. "MADE IN CHINA": "",
  198. }
  199. _pp = PictureProcessing()
  200. text = f"{mapping[item]} {self.get_text_value(item)}"
  201. duanluo = ""
  202. sum_width = 0
  203. line_count = 0
  204. for char in text:
  205. text_width = font.getlength(char)
  206. sum_width += text_width
  207. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  208. line_count += 1
  209. sum_width = 0
  210. duanluo += "\n"
  211. duanluo += char
  212. value = (0,0)
  213. if idx ==0 or idx == len(dataList)-1:
  214. value = (-5,0)
  215. _pp = _pp.get_text_image_advanced(
  216. value=value,
  217. font=font,
  218. text=duanluo,
  219. spacing=10,
  220. fill=(0, 0, 0),
  221. return_mode="min_image_high",
  222. )
  223. height_total += _pp.height + line_space
  224. text_list.append(_pp)
  225. pp_bg_jpg = PictureProcessing(
  226. "RGB", (base_bg_width, height_total), (255, 255, 255)
  227. )
  228. goods_art_no_list = list(self.data.keys())
  229. current_y = 0
  230. for index, text_item in enumerate(text_list):
  231. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(20, current_y))
  232. # 更新 Y 坐标为当前项的底部位置
  233. current_y += text_item.height + line_space
  234. pp_jpg, pp_png = self.image_one_pic(
  235. return_orign=True,
  236. goods_art_no=goods_art_no_list[0],
  237. name="俯视",
  238. )
  239. pp_png = pp_png.resize(value=base_limit_width)
  240. background_size = 0
  241. if pp_png.height > pp_bg_jpg.height:
  242. background_size = pp_png.height + 100
  243. else:
  244. background_size = pp_bg_jpg.height + 100
  245. pp_image_jpg = PictureProcessing(
  246. "RGB", (base_bg_width, background_size), (255, 255, 255)
  247. )
  248. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  249. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  250. detailed_images.append(pp_image_jpg)
  251. return PictureProcessing(im=self.add_pic(detailed_images))