🎨 优化工具类代码

This commit is contained in:
HibiKier 2024-11-21 15:10:07 +08:00
parent 03d8b3aafd
commit af8f58b61e
2 changed files with 24 additions and 3 deletions

View File

@ -156,7 +156,8 @@ class BuildImage:
""" """
if not img_list: if not img_list:
raise ValueError("贴图类别为空...") raise ValueError("贴图类别为空...")
width, height = img_list[0].size width = max(img.size[0] for img in img_list)
height = max(img.size[1] for img in img_list)
background_width = width * row + space * (row - 1) + padding * 2 background_width = width * row + space * (row - 1) + padding * 2
row_count = math.ceil(len(img_list) / row) row_count = math.ceil(len(img_list) / row)
if row_count == 1: if row_count == 1:
@ -168,12 +169,22 @@ class BuildImage:
background_width, background_height, color=color, background=background background_width, background_height, color=color, background=background
) )
_cur_width, _cur_height = padding, padding _cur_width, _cur_height = padding, padding
for img in img_list: row_num = 0
for i in range(len(img_list)):
row_num += 1
img: Self | tImage = img_list[i]
await background_image.paste(img, (_cur_width, _cur_height)) await background_image.paste(img, (_cur_width, _cur_height))
_cur_width += space + img.width _cur_width += space + img.width
if _cur_width + padding >= background_image.width: next_image_width = 0
if i != len(img_list) - 1:
next_image_width = img_list[i + 1].width
if (
row_num == row
or _cur_width + padding + next_image_width >= background_image.width + 1
):
_cur_height += space + img.height _cur_height += space + img.height
_cur_width = padding _cur_width = padding
row_num = 0
return background_image return background_image
@classmethod @classmethod
@ -719,3 +730,11 @@ class BuildImage:
bytes: bytes bytes: bytes
""" """
return self.markImg.tobytes() return self.markImg.tobytes()
def copy(self) -> "BuildImage":
"""复制
返回:
BuildImage: Self
"""
return BuildImage.open(self.pic2bytes())

View File

@ -124,6 +124,8 @@ class MessageUtils:
_message[i] = Image( _message[i] = Image(
raw=BuildImage.open(_message[i]).pic2bytes() raw=BuildImage.open(_message[i]).pic2bytes()
) )
elif isinstance(_message[i], BuildImage):
_message[i] = Image(raw=_message[i].pic2bytes())
node_list.append( node_list.append(
CustomNode(uid=uin, name=name, content=UniMessage(_message)) CustomNode(uid=uin, name=name, content=UniMessage(_message))
) )