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