""" 步骤: 1、整理需要处理的款号图-输出款号图文件夹 2、整理所有相关的图片作为素材图 3、按要求进行拼接 """ import os from PIL import ImageFont, ImageDraw import sys import settings # 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("图片路径") # file_path = file_path.replace("\\", "/") parent_folder = os.path.dirname(file_path) if os.path.exists(parent_folder) == False: # 确保目标目录存在 os.makedirs(parent_folder, exist_ok=True) print("file_path是否存在",parent_folder, os.path.exists(parent_folder)) file_extension = file_path.split(".")[-1] format_mapping = {"jpg": "JPEG", "png": "PNG", "webp": "WEBP", "avif": "AVIF"} file_format = format_mapping[file_extension] img.save(file_path, format=file_format) img.show() # if test: # self.run_test() # else: # self.run_all() 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): info = self.get_text_value(item) if info == None or info == "": continue _pp = PictureProcessing() 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 "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:", "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))