detail_xinjunlue1.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. ]
  60. if test:
  61. self.run_test()
  62. else:
  63. self.run_all()
  64. def run_test(self):
  65. detailed_images = []
  66. detailed_images.append(self.deal_pic_1())
  67. img = self.add_pic(detailed_images)
  68. if img:
  69. self.create_folder(r"{}/{}".format(self.out_put_dir, template_name))
  70. # print("保存图片成功", img.show())
  71. img.save(
  72. r"{}/{}/{}.jpg".format(
  73. self.out_put_dir, template_name, self.goods_no, format="JPEG"
  74. )
  75. )
  76. img.show()
  77. #
  78. def deal_pic_1(self):
  79. detailed_images = []
  80. base_bg_width = 1200
  81. base_limit_width = base_bg_width / 2
  82. font = ImageFont.truetype("msyh.ttc", 20)
  83. dataList = ["款号", "面料", "内里", "垫脚", "大底", "饰扣", "客户", "日期"]
  84. height_total = 0
  85. text_list = []
  86. line_space = 15
  87. for idx, item in enumerate(dataList):
  88. info = self.get_text_value(item)
  89. if info == None or info == "":
  90. continue
  91. _pp = PictureProcessing()
  92. text = f"{item}: {self.get_text_value(item)}"
  93. duanluo = ""
  94. sum_width = 0
  95. line_count = 0
  96. for char in text:
  97. text_width = font.getlength(char)
  98. sum_width += text_width
  99. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  100. line_count += 1
  101. sum_width = 0
  102. duanluo += '\n'
  103. duanluo += char
  104. _pp = _pp.get_text_image_advanced(
  105. font=font,
  106. text=duanluo,
  107. spacing=10,
  108. fill=(0, 0, 0),
  109. return_mode="min_image_high",
  110. )
  111. height_total += _pp.height + line_space
  112. text_list.append(_pp)
  113. pp_bg_jpg = PictureProcessing(
  114. "RGB", (base_bg_width, height_total), (255, 255, 255)
  115. )
  116. goods_art_no_list = list(self.data.keys())
  117. current_y = 0
  118. for index, text_item in enumerate(text_list):
  119. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(10, current_y))
  120. # 更新 Y 坐标为当前项的底部位置
  121. current_y += text_item.height + line_space
  122. pp_jpg, pp_png = self.image_one_pic(
  123. return_orign=True,
  124. goods_art_no=goods_art_no_list[0],
  125. name="俯视",
  126. )
  127. pp_png = pp_png.resize(value=base_limit_width)
  128. background_size = 0
  129. if pp_png.height > pp_bg_jpg.height:
  130. background_size = pp_png.height+100
  131. else:
  132. background_size = pp_bg_jpg.height + 100
  133. pp_image_jpg = PictureProcessing(
  134. "RGB", (base_bg_width, background_size), (255, 255, 255)
  135. )
  136. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  137. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  138. detailed_images.append(pp_image_jpg)
  139. return PictureProcessing(im=self.add_pic(detailed_images))
  140. def deal_pic_1(self):
  141. detailed_images = []
  142. base_bg_width = 1200
  143. base_limit_width = base_bg_width / 2
  144. font = ImageFont.truetype("msyh.ttc", 20)
  145. dataList = ["款号", "面料", "内里", "垫脚", "大底", "饰扣", "客户", "日期"]
  146. height_total = 0
  147. text_list = []
  148. line_space = 15
  149. for idx, item in enumerate(dataList):
  150. info = self.get_text_value(item)
  151. if info == None or info == "":
  152. continue
  153. _pp = PictureProcessing()
  154. text = f"{item}: {self.get_text_value(item)}"
  155. duanluo = ""
  156. sum_width = 0
  157. line_count = 0
  158. for char in text:
  159. text_width = font.getlength(char)
  160. sum_width += text_width
  161. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  162. line_count += 1
  163. sum_width = 0
  164. duanluo += "\n"
  165. duanluo += char
  166. _pp = _pp.get_text_image_advanced(
  167. font=font,
  168. text=duanluo,
  169. spacing=10,
  170. fill=(0, 0, 0),
  171. return_mode="min_image_high",
  172. )
  173. height_total += _pp.height + line_space
  174. text_list.append(_pp)
  175. pp_bg_jpg = PictureProcessing(
  176. "RGB", (base_bg_width, height_total), (255, 255, 255)
  177. )
  178. goods_art_no_list = list(self.data.keys())
  179. current_y = 0
  180. for index, text_item in enumerate(text_list):
  181. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(20, current_y))
  182. # 更新 Y 坐标为当前项的底部位置
  183. current_y += text_item.height + line_space
  184. pp_jpg, pp_png = self.image_one_pic(
  185. return_orign=True,
  186. goods_art_no=goods_art_no_list[0],
  187. name="俯视",
  188. )
  189. pp_png = pp_png.resize(value=base_limit_width)
  190. background_size = 0
  191. if pp_png.height > pp_bg_jpg.height:
  192. background_size = pp_png.height + 100
  193. else:
  194. background_size = pp_bg_jpg.height + 100
  195. pp_image_jpg = PictureProcessing(
  196. "RGB", (base_bg_width, background_size), (255, 255, 255)
  197. )
  198. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  199. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  200. detailed_images.append(pp_image_jpg)
  201. return PictureProcessing(im=self.add_pic(detailed_images))