🐛 修复获取sruid不正常的BUG

This commit is contained in:
Wuyi无疑 2023-08-06 16:24:37 +08:00
parent 1ee193110f
commit 542beff78c
2 changed files with 44 additions and 11 deletions

View File

@ -122,7 +122,7 @@ class SQLA:
return await GsBind.get_uid_list_by_game(user_id, self.bot_id, 'sr') return await GsBind.get_uid_list_by_game(user_id, self.bot_id, 'sr')
async def get_bind_sruid(self, user_id: str) -> Optional[str]: async def get_bind_sruid(self, user_id: str) -> Optional[str]:
return await GsBind.get_uid_by_game(user_id, self.bot_id) return await GsBind.get_uid_by_game(user_id, self.bot_id, 'sr')
async def switch_uid( async def switch_uid(
self, user_id: str, uid: Optional[str] = None self, user_id: str, uid: Optional[str] = None

View File

@ -42,12 +42,22 @@ def draw_center_text_by_line(
) -> float: ) -> float:
pun = "。!?;!?" pun = "。!?;!?"
x, y = pos x, y = pos
_, h = font.getsize('X')
if hasattr(font, 'getsize'):
_, h = font.getsize('X')
else:
bbox = font.getbbox('X')
_, h = 0, bbox[3] - bbox[1]
line = '' line = ''
lenth = 0 lenth = 0
anchor = 'la' if not_center else 'mm' anchor = 'la' if not_center else 'mm'
for char in text: for char in text:
size, _ = font.getsize(char) # 获取当前字符的宽度 if hasattr(font, 'getsize'):
size, _ = font.getsize(char) # 获取当前字符的宽度
else:
bbox = font.getbbox(char)
size, _ = bbox[2] - bbox[0], bbox[3] - bbox[1]
lenth += size lenth += size
line += char line += char
if lenth < max_length and char not in pun and char != '\n': if lenth < max_length and char not in pun and char != '\n':
@ -77,7 +87,13 @@ def draw_text_by_line(
line_space 行间距, 单位像素, 默认是字体高度的0.3 line_space 行间距, 单位像素, 默认是字体高度的0.3
""" """
x, y = pos x, y = pos
_, h = font.getsize('X')
if hasattr(font, 'getsize'):
_, h = font.getsize('X')
else:
bbox = font.getbbox('X')
_, h = 0, bbox[3] - bbox[1]
if line_space is None: if line_space is None:
y_add = math.ceil(1.3 * h) y_add = math.ceil(1.3 * h)
else: else:
@ -86,14 +102,23 @@ def draw_text_by_line(
row = "" # 存储本行文字 row = "" # 存储本行文字
length = 0 # 记录本行长度 length = 0 # 记录本行长度
for character in text: for character in text:
w, h = font.getsize(character) # 获取当前字符的宽度 if hasattr(font, 'getsize'):
w, h = font.getsize(character) # 获取当前字符的宽度
else:
bbox = font.getbbox('X')
w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
if length + w * 2 <= max_length: if length + w * 2 <= max_length:
row += character row += character
length += w length += w
else: else:
row += character row += character
if center: if center:
font_size = font.getsize(row) if hasattr(font, 'getsize'):
font_size = font.getsize(row)
else:
bbox = font.getbbox(character)
font_size = bbox[2] - bbox[0], bbox[3] - bbox[1]
x = math.ceil((img.size[0] - font_size[0]) / 2) x = math.ceil((img.size[0] - font_size[0]) / 2)
draw.text((x, y), row, font=font, fill=fill) draw.text((x, y), row, font=font, fill=fill)
row = '' row = ''
@ -101,7 +126,11 @@ def draw_text_by_line(
y += y_add y += y_add
if row != "": if row != "":
if center: if center:
font_size = font.getsize(row) if hasattr(font, 'getsize'):
font_size = font.getsize(row)
else:
bbox = font.getbbox(row)
font_size = bbox[2] - bbox[0], bbox[3] - bbox[1]
x = math.ceil((img.size[0] - font_size[0]) / 2) x = math.ceil((img.size[0] - font_size[0]) / 2)
draw.text((x, y), row, font=font, fill=fill) draw.text((x, y), row, font=font, fill=fill)
return y return y
@ -154,6 +183,7 @@ async def draw_pic_with_ring(
pic: Image.Image, pic: Image.Image,
size: int, size: int,
bg_color: Optional[Tuple[int, int, int]] = None, bg_color: Optional[Tuple[int, int, int]] = None,
is_ring: bool = True,
): ):
''' '''
:说明: :说明:
@ -171,7 +201,6 @@ async def draw_pic_with_ring(
mask_pic = Image.open(TEXT_PATH / 'mask.png') mask_pic = Image.open(TEXT_PATH / 'mask.png')
img = Image.new('RGBA', (size, size)) img = Image.new('RGBA', (size, size))
mask = mask_pic.resize((size, size)) mask = mask_pic.resize((size, size))
ring = ring_pic.resize((size, size))
resize_pic = crop_center_img(pic, size, size) resize_pic = crop_center_img(pic, size, size)
if bg_color: if bg_color:
img_color = Image.new('RGBA', (size, size), bg_color) img_color = Image.new('RGBA', (size, size), bg_color)
@ -179,7 +208,11 @@ async def draw_pic_with_ring(
img.paste(img_color, (0, 0), mask) img.paste(img_color, (0, 0), mask)
else: else:
img.paste(resize_pic, (0, 0), mask) img.paste(resize_pic, (0, 0), mask)
img.paste(ring, (0, 0), ring)
if is_ring:
ring = ring_pic.resize((size, size))
img.paste(ring, (0, 0), ring)
return img return img
@ -193,13 +226,13 @@ def crop_center_img(
new_w = math.ceil(based_h * float(scale_f)) new_w = math.ceil(based_h * float(scale_f))
new_h = math.ceil(based_w / float(scale_f)) new_h = math.ceil(based_w / float(scale_f))
if scale_f > based_scale: if scale_f > based_scale:
resize_img = img.resize((new_w, based_h), Image.ANTIALIAS) resize_img = img.resize((new_w, based_h), Image.Resampling.LANCZOS)
x1 = int(new_w / 2 - based_w / 2) x1 = int(new_w / 2 - based_w / 2)
y1 = 0 y1 = 0
x2 = int(new_w / 2 + based_w / 2) x2 = int(new_w / 2 + based_w / 2)
y2 = based_h y2 = based_h
else: else:
resize_img = img.resize((based_w, new_h), Image.ANTIALIAS) resize_img = img.resize((based_w, new_h), Image.Resampling.LANCZOS)
x1 = 0 x1 = 0
y1 = int(new_h / 2 - based_h / 2) y1 = int(new_h / 2 - based_h / 2)
x2 = based_w x2 = based_w