detail_xinjunlue1.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "日期",
  111. ]
  112. height_total = 0
  113. text_list = []
  114. line_space = 15
  115. for idx, item in enumerate(dataList):
  116. if item == "颜色":
  117. item = "颜色名称"
  118. info = self.get_text_value(item)
  119. if info == None or info == "":
  120. continue
  121. _pp = PictureProcessing()
  122. if item == "颜色":
  123. item = "颜色名称"
  124. text = f"{item}: {self.get_text_value(item)}"
  125. duanluo = ""
  126. sum_width = 0
  127. line_count = 0
  128. for char in text:
  129. text_width = font.getlength(char)
  130. sum_width += text_width
  131. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  132. line_count += 1
  133. sum_width = 0
  134. duanluo += '\n'
  135. duanluo += char
  136. _pp = _pp.get_text_image_advanced(
  137. font=font,
  138. text=duanluo,
  139. spacing=10,
  140. fill=(0, 0, 0),
  141. return_mode="min_image_high",
  142. )
  143. height_total += _pp.height + line_space
  144. text_list.append(_pp)
  145. pp_bg_jpg = PictureProcessing(
  146. "RGB", (base_bg_width, height_total), (255, 255, 255)
  147. )
  148. goods_art_no_list = list(self.data.keys())
  149. current_y = 0
  150. for index, text_item in enumerate(text_list):
  151. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(10, current_y))
  152. # 更新 Y 坐标为当前项的底部位置
  153. current_y += text_item.height + line_space
  154. pp_jpg, pp_png = self.image_one_pic(
  155. return_orign=True,
  156. goods_art_no=goods_art_no_list[0],
  157. name="俯视",
  158. )
  159. pp_png = pp_png.resize(value=base_limit_width)
  160. background_size = 0
  161. if pp_png.height > pp_bg_jpg.height:
  162. background_size = pp_png.height+100
  163. else:
  164. background_size = pp_bg_jpg.height + 100
  165. pp_image_jpg = PictureProcessing(
  166. "RGB", (base_bg_width, background_size), (255, 255, 255)
  167. )
  168. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  169. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  170. detailed_images.append(pp_image_jpg)
  171. return PictureProcessing(im=self.add_pic(detailed_images))
  172. def deal_pic_2(self):
  173. detailed_images = []
  174. base_bg_width = 1200
  175. base_limit_width = base_bg_width / 2
  176. font = ImageFont.truetype("arial.ttf", 20)
  177. dataList = [
  178. "JEFFREY CAMPBELL",
  179. "Supplier",
  180. "款号", # Fty No
  181. "颜色名称", # Fty No
  182. "Material",
  183. "Lining&Sock",
  184. "码数", # Size
  185. "日期", # Date
  186. "Remark",
  187. "MADE IN CHINA",
  188. ]
  189. height_total = 0
  190. text_list = []
  191. line_space = 15
  192. for idx, item in enumerate(dataList):
  193. info = self.get_text_value(item)
  194. if info == None or info == "":
  195. continue
  196. mapping = {
  197. "JEFFREY CAMPBELL": "",
  198. "Supplier": "Supplier:",
  199. "款号": "Fty No:",
  200. "颜色名称": "Color:",
  201. "Material": "Material:",
  202. "Lining&Sock": "Lining&Sock:",
  203. "码数": "Size:",
  204. "日期": "Date:",
  205. "Remark": "Remark:",
  206. "MADE IN CHINA": "",
  207. }
  208. _pp = PictureProcessing()
  209. text = f"{mapping[item]} {self.get_text_value(item)}"
  210. duanluo = ""
  211. sum_width = 0
  212. line_count = 0
  213. for char in text:
  214. text_width = font.getlength(char)
  215. sum_width += text_width
  216. if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
  217. line_count += 1
  218. sum_width = 0
  219. duanluo += "\n"
  220. duanluo += char
  221. value = (0,0)
  222. if idx ==0 or idx == len(dataList)-1:
  223. value = (-5,0)
  224. _pp = _pp.get_text_image_advanced(
  225. value=value,
  226. font=font,
  227. text=duanluo,
  228. spacing=10,
  229. fill=(0, 0, 0),
  230. return_mode="min_image_high",
  231. )
  232. height_total += _pp.height + line_space
  233. text_list.append(_pp)
  234. pp_bg_jpg = PictureProcessing(
  235. "RGB", (base_bg_width, height_total), (255, 255, 255)
  236. )
  237. goods_art_no_list = list(self.data.keys())
  238. current_y = 0
  239. for index, text_item in enumerate(text_list):
  240. pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(20, current_y))
  241. # 更新 Y 坐标为当前项的底部位置
  242. current_y += text_item.height + line_space
  243. pp_jpg, pp_png = self.image_one_pic(
  244. return_orign=True,
  245. goods_art_no=goods_art_no_list[0],
  246. name="俯视",
  247. )
  248. pp_png = pp_png.resize(value=base_limit_width)
  249. background_size = 0
  250. if pp_png.height > pp_bg_jpg.height:
  251. background_size = pp_png.height + 100
  252. else:
  253. background_size = pp_bg_jpg.height + 100
  254. pp_image_jpg = PictureProcessing(
  255. "RGB", (base_bg_width, background_size), (255, 255, 255)
  256. )
  257. pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
  258. pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
  259. detailed_images.append(pp_image_jpg)
  260. return PictureProcessing(im=self.add_pic(detailed_images))