| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- """
- 步骤:
- 1、整理需要处理的款号图-输出款号图文件夹
- 2、整理所有相关的图片作为素材图
- 3、按要求进行拼接
- """
- import os
- from PIL import ImageFont, ImageDraw
- import sys
- import settings
- from middleware import UnicornException
- # from PIL import Image, ImageDraw, ImageFont
- # from module.view_control.generate_goods_no_detail_pic.detail_generate_base import DetailBase
- # from module.view_control.generate_goods_no_detail_pic.pic_deal import PictureProcessing
- is_test_plugins = False
- try:
- is_test_plugins = settings.is_test_plugins
- except:
- is_test_plugins = False
- if is_test_plugins:
- from custom_plugins.plugins_mode.detail_generate_base import DetailBase
- from custom_plugins.plugins_mode.pic_deal import PictureProcessing
- else:
- from plugins_mode.detail_generate_base import DetailBase
- from plugins_mode.pic_deal import PictureProcessing
- plugins_name = "详情模板"
- company_name_list = ["全部"]
- template_name = "xinjunlue-1"
- # 乐福鞋 详情页
- class DetailPicGet(DetailBase):
- need_view = ["俯视", "侧视", "后跟", "鞋底", "内里"]
- root = r"{}\resources\detail_temp\zhudi\1".format(os.getcwd())
- def __init__(
- self,
- goods_no,
- goods_no_value: dict,
- out_put_dir,
- windows=None,
- test=False,
- excel_data=None,
- assigned_page_list=None,
- output_queue=None,
- **kwargs
- ):
- super().__init__(
- goods_no,
- goods_no_value,
- out_put_dir,
- windows=windows,
- excel_data=excel_data,
- assigned_page_list=assigned_page_list,
- output_queue=output_queue,
- )
- self.template_name = template_name
- self.root = r"{}\resources\detail_temp\zhudi\1".format(os.getcwd())
- print(f"run {template_name} ")
- self.base_bg_color = (255, 255, 255)
- self.deal_pic_func_list = [
- self.deal_pic_1(),
- self.deal_pic_2(),
- ]
- img = self.add_pic(self.deal_pic_func_list)
- file_path = self.get_text_value("图片路径")
- if file_path == None or file_path == "":
- raise UnicornException("图片路径不能为空")
- parent_folder = os.path.dirname(file_path)
- try:
- if os.path.exists(parent_folder) == False:
- # 确保目标目录存在
- os.makedirs(parent_folder, exist_ok=True)
- file_extension = file_path.split(".")[-1]
- format_mapping = {"jpg": "JPEG", "png": "PNG", "webp": "WEBP", "avif": "AVIF"}
- file_format = format_mapping[file_extension.lower()]
- img.save(file_path, format=file_format)
- except KeyError:
- raise UnicornException(f"不支持的文件格式: {file_extension}")
- except Exception as e:
- raise UnicornException(f"保存图片时发生错误: {e},请检查路径")
- def run_test(self):
- detailed_images = []
- detailed_images.append(self.deal_pic_1())
- detailed_images.append(self.deal_pic_2())
- img = self.add_pic(detailed_images)
- if img:
- self.create_folder(r"{}/{}".format(self.out_put_dir, template_name))
- # print("保存图片成功", img.show())
- img.save(
- r"{}/{}/{}.jpg".format(
- self.out_put_dir, template_name, self.goods_no, format="JPEG"
- )
- )
- img.show()
- #
- def deal_pic_1(self):
- detailed_images = []
- base_bg_width = 1200
- base_limit_width = base_bg_width / 2
- font = ImageFont.truetype("msyh.ttc", 20)
- dataList = [
- "款号",
- "楦号",
- "颜色",
- "面料",
- "内里",
- "垫脚",
- "大底",
- "饰扣",
- "客户",
- "码数",
- "日期",
- ]
- height_total = 0
- text_list = []
- line_space = 15
- for idx, item in enumerate(dataList):
- if item == "颜色":
- item = "颜色名称"
- info = self.get_text_value(item)
- if info == None or info == "":
- continue
- _pp = PictureProcessing()
- if item == "颜色":
- item = "颜色名称"
- text = f"{item}: {self.get_text_value(item)}"
- duanluo = ""
- sum_width = 0
- line_count = 0
- for char in text:
- text_width = font.getlength(char)
- sum_width += text_width
- if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
- line_count += 1
- sum_width = 0
- duanluo += '\n'
- duanluo += char
- _pp = _pp.get_text_image_advanced(
- font=font,
- text=duanluo,
- spacing=10,
- fill=(0, 0, 0),
- return_mode="min_image_high",
- )
- height_total += _pp.height + line_space
- text_list.append(_pp)
- pp_bg_jpg = PictureProcessing(
- "RGB", (base_bg_width, height_total), (255, 255, 255)
- )
- goods_art_no_list = list(self.data.keys())
- current_y = 0
- for index, text_item in enumerate(text_list):
- pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(10, current_y))
- # 更新 Y 坐标为当前项的底部位置
- current_y += text_item.height + line_space
- pp_jpg, pp_png = self.image_one_pic(
- return_orign=True,
- goods_art_no=goods_art_no_list[0],
- name="俯视",
- )
- pp_png = pp_png.resize(value=base_limit_width)
- background_size = 0
- if pp_png.height > pp_bg_jpg.height:
- background_size = pp_png.height+100
- else:
- background_size = pp_bg_jpg.height + 100
- pp_image_jpg = PictureProcessing(
- "RGB", (base_bg_width, background_size), (255, 255, 255)
- )
- pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
- pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
- detailed_images.append(pp_image_jpg)
- return PictureProcessing(im=self.add_pic(detailed_images))
- def deal_pic_2(self):
- detailed_images = []
- base_bg_width = 1200
- base_limit_width = base_bg_width / 2
- font = ImageFont.truetype("arial.ttf", 20)
- dataList = [
- "JEFFREY CAMPBELL",
- "Supplier",
- "款号", # Fty No
- "颜色名称", # Fty No
- "Material",
- "Lining&Sock",
- "码数", # Size
- "日期", # Date
- "Remark",
- "MADE IN CHINA",
- ]
- height_total = 0
- text_list = []
- line_space = 15
- for idx, item in enumerate(dataList):
- info = self.get_text_value(item)
- if info == None or info == "":
- continue
- mapping = {
- "JEFFREY CAMPBELL": "",
- "Supplier": "Supplier:",
- "款号": "Fty No:",
- "颜色名称": "Color:",
- "Material": "Material:",
- "Lining&Sock": "Lining&Sock:",
- "码数": "Size:",
- "日期": "Date:",
- "Remark": "Remark:",
- "MADE IN CHINA": "",
- }
- _pp = PictureProcessing()
- text = f"{mapping[item]} {self.get_text_value(item)}"
- duanluo = ""
- sum_width = 0
- line_count = 0
- for char in text:
- text_width = font.getlength(char)
- sum_width += text_width
- if sum_width > base_limit_width: # 超过预设宽度就修改段落 以及当前行数
- line_count += 1
- sum_width = 0
- duanluo += "\n"
- duanluo += char
- value = (0,0)
- if idx ==0 or idx == len(dataList)-1:
- value = (-5,0)
- _pp = _pp.get_text_image_advanced(
- value=value,
- font=font,
- text=duanluo,
- spacing=10,
- fill=(0, 0, 0),
- return_mode="min_image_high",
- )
- height_total += _pp.height + line_space
- text_list.append(_pp)
- pp_bg_jpg = PictureProcessing(
- "RGB", (base_bg_width, height_total), (255, 255, 255)
- )
- goods_art_no_list = list(self.data.keys())
- current_y = 0
- for index, text_item in enumerate(text_list):
- pp_bg_jpg = pp_bg_jpg.paste_img(top_img=text_item, value=(20, current_y))
- # 更新 Y 坐标为当前项的底部位置
- current_y += text_item.height + line_space
- pp_jpg, pp_png = self.image_one_pic(
- return_orign=True,
- goods_art_no=goods_art_no_list[0],
- name="俯视",
- )
- pp_png = pp_png.resize(value=base_limit_width)
- background_size = 0
- if pp_png.height > pp_bg_jpg.height:
- background_size = pp_png.height + 100
- else:
- background_size = pp_bg_jpg.height + 100
- pp_image_jpg = PictureProcessing(
- "RGB", (base_bg_width, background_size), (255, 255, 255)
- )
- pp_image_jpg.paste_img(top_img=pp_bg_jpg, base="wc", value=(0, 0))
- pp_image_jpg.paste_img(top_img=pp_png, base="ec", value=(0, 0))
- detailed_images.append(pp_image_jpg)
- return PictureProcessing(im=self.add_pic(detailed_images))
|