From 4d6f747d79e91b913643035eb710f92d44b75b28 Mon Sep 17 00:00:00 2001 From: Chinesehou97 <202169359@qq.com> Date: Wed, 4 Sep 2024 15:49:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=96=87=E5=AD=97=E4=B8=8D?= =?UTF-8?q?=E5=B1=85=E4=B8=AD=E4=B8=8D=E8=87=AA=E5=8A=A8=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- output.json | 90 +++++++++++++++---------------- src/mulimgviewer/src/utils_img.py | 42 ++++++++++----- 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/output.json b/output.json index 7a9bc5c..0c46ff1 100644 --- a/output.json +++ b/output.json @@ -1,46 +1,46 @@ { - "row_col": "2,2", - "row_col_one_img": "1,1", - "show_scale": "0.32,0.32", - "row_col_img_unit": "3,1", - "gap": "10,10,5,5,3,3,2,2", - "magnifer_row_col": "1,1", - "output_scale": "1,1", - "img_resolution": "-1,-1", - "magnifer_resolution": "-1,-1", - "magnifier_show_scale": "1,1", - "line_width": "2,2", - "magnifier_out_scale": "1,1", - "title_font_size": "20", - "box_position": 0, - "choice_normalized_size": 0, - "choice_output": 0, - "image_interp": 2, - "Magnifier_format": 1, - "title_font": 0, - "parallel_sequential": false, - "parallel_to_sequential": false, - "auto_save_all": false, - "move_file": false, - "img_vertical": false, - "one_img_vertical": false, - "img_unit_vertical": false, - "magnifer_vertical": true, - "show_original": true, - "show_magnifer": true, - "title_show": true, - "auto_layout_check": false, - "one_img": false, - "onetitle": false, - "customfunc": false, - "show_box": true, - "show_box_in_crop": true, - "select_img_box": false, - "title_auto": false, - "title_exif": false, - "title_show_parent": false, - "title_show_prefix": true, - "title_show_name": true, - "title_show_suffix": false, - "title_down_up": false -} \ No newline at end of file + "row_col": "1,1", + "row_col_one_img": "1,1", + "show_scale": "1,1", + "row_col_img_unit": "3,1", + "gap": "10,10,5,5,3,3,2,2", + "magnifer_row_col": "1,1", + "output_scale": "1,1", + "img_resolution": "-1,-1", + "magnifer_resolution": "-1,-1", + "magnifier_show_scale": "-1,-1", + "line_width": "2,2", + "magnifier_out_scale": "1,1", + "title_font_size": "20", + "box_position": 0, + "choice_normalized_size": 0, + "choice_output": 0, + "image_interp": 2, + "Magnifier_format": 1, + "title_font": 0, + "parallel_sequential": false, + "parallel_to_sequential": false, + "auto_save_all": false, + "move_file": false, + "img_vertical": false, + "one_img_vertical": false, + "img_unit_vertical": false, + "magnifer_vertical": true, + "show_original": true, + "show_magnifer": true, + "title_show": true, + "auto_layout_check": false, + "one_img": false, + "onetitle": false, + "customfunc": false, + "show_box": true, + "show_box_in_crop": true, + "select_img_box": false, + "title_auto": false, + "title_exif": false, + "title_show_parent": false, + "title_show_prefix": true, + "title_show_name": true, + "title_show_suffix": false, + "title_down_up": false + } diff --git a/src/mulimgviewer/src/utils_img.py b/src/mulimgviewer/src/utils_img.py index 1044560..d14292e 100644 --- a/src/mulimgviewer/src/utils_img.py +++ b/src/mulimgviewer/src/utils_img.py @@ -3,7 +3,7 @@ import os from pathlib import Path from shutil import copyfile, move - +import textwrap from .custom_func.main import main as main_custom_func import numpy as np import piexif @@ -567,7 +567,7 @@ def cal_txt_size_adjust_title(self, title_list, standard_size, font, font_size): split_num = 2 title = title_list[i] str_ = title_list[i] - while title_size[i, 0] > standard_size: + while title_size[i*2+1, 0] > standard_size: ids = [0] + [(i+1)*int(len(title)/split_num) for i in range(split_num-1)] str_ = "" @@ -583,7 +583,7 @@ def cal_txt_size_adjust_title(self, title_list, standard_size, font, font_size): size_edit = draw.multiline_textbbox((0,0),str_, font) size_edit = np.array(size_edit) size_edit = size_edit.reshape(-1, 2) - title_size[i, :] = size_edit[0,:] + title_size[i*2+1, :] = size_edit[1,:] split_num = split_num+1 if split_num > len(title): break @@ -997,15 +997,33 @@ def title_preprocessing(self, img, id): img = Image.new('RGBA', tuple(title_max_size), self.gap_color) draw = ImageDraw.Draw(img) title_size = self.title_size[id*2+1, :] - delta_x = int((title_max_size[0]-title_size[0])/2) - if self.title_setting[2]: - # up - draw.multiline_text( - (delta_x, 0), self.title_list[id], font=self.font, fill=self.text_color) - else: - # down - draw.multiline_text( - (delta_x, 0), self.title_list[id], font=self.font, fill=self.text_color) + delta_x = max(0,int((title_max_size[0]-title_size[0])/2)) + one_size = int(int(self.title_setting[8])/2)#int(title_size[0]/int(len(self.title_list[id]))) + wrapper = textwrap.TextWrapper(width=int(int(title_max_size[0])/int(one_size))) # 设置换行的宽度 + lines = wrapper.wrap(text=self.title_list[id]) + if delta_x + title_size[0] > title_max_size[0]: + delta_x = 0 + y = 0 + # 遍历处理过的行进行绘制 + for line in lines: + if delta_x + len(line )*one_size > title_max_size[0]: + delta_x = 0 + if self.title_setting[2]: + # up + draw.text((delta_x, y), line, align="center",font=self.font, fill=self.text_color) + else: + # down + draw.text((delta_x, y), line, align="center",font=self.font, fill=self.text_color) + y += int(self.title_setting[8]) # 增加y轴偏移量,确保每行文本不重叠 + + # if self.title_setting[2]: + # # up + # draw.multiline_text( + # (delta_x, 0), self.title_list[id], font=self.font, fill=self.text_color,align="left") + # else: + # # down (anchor=None,spacing=0,align="left",direction=None,features=None) + # draw.multiline_text( + # (delta_x, 0), self.title_list[id], font=self.font, fill=self.text_color,align="left") return img def title_init(self, width_2, height_2):