mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-10 13:10:57 +00:00
perf: 优化图片渲染
This commit is contained in:
+6
-1
@@ -1,4 +1,5 @@
|
|||||||
# 长消息处理相关
|
# 长消息处理相关
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import base64
|
import base64
|
||||||
@@ -54,7 +55,10 @@ def text_to_image(text: str) -> MessageComponent:
|
|||||||
|
|
||||||
# 删除图片
|
# 删除图片
|
||||||
os.remove(img_path)
|
os.remove(img_path)
|
||||||
os.remove(compressed_path)
|
|
||||||
|
# 判断compressed_path是否存在
|
||||||
|
if os.path.exists(compressed_path):
|
||||||
|
os.remove(compressed_path)
|
||||||
# 返回图片
|
# 返回图片
|
||||||
return Image(base64=b64.decode('utf-8'))
|
return Image(base64=b64.decode('utf-8'))
|
||||||
|
|
||||||
@@ -68,6 +72,7 @@ def check_text(text: str) -> list:
|
|||||||
if not hasattr(config, 'blob_message_strategy'):
|
if not hasattr(config, 'blob_message_strategy'):
|
||||||
raise AttributeError('未定义长消息处理策略')
|
raise AttributeError('未定义长消息处理策略')
|
||||||
|
|
||||||
|
# logging.info("长消息: {}".format(text))
|
||||||
if config.blob_message_strategy == 'image':
|
if config.blob_message_strategy == 'image':
|
||||||
# 转换成图片
|
# 转换成图片
|
||||||
return [text_to_image(text)]
|
return [text_to_image(text)]
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ def compress_image(infile, outfile='', kb=100, step=20, quality=90):
|
|||||||
"""
|
"""
|
||||||
o_size = get_size(infile)
|
o_size = get_size(infile)
|
||||||
if o_size <= kb:
|
if o_size <= kb:
|
||||||
return infile
|
return infile, o_size
|
||||||
outfile = get_outfile(infile, outfile)
|
outfile = get_outfile(infile, outfile)
|
||||||
while o_size > kb:
|
while o_size > kb:
|
||||||
im = Image.open(infile)
|
im = Image.open(infile)
|
||||||
@@ -82,9 +82,10 @@ def compress_image(infile, outfile='', kb=100, step=20, quality=90):
|
|||||||
return outfile, get_size(outfile)
|
return outfile, get_size(outfile)
|
||||||
|
|
||||||
|
|
||||||
def text_to_image(text_str, save_as="temp.png", width=800):
|
def text_to_image(text_str: str, save_as="temp.png", width=800):
|
||||||
global text_render_font
|
global text_render_font
|
||||||
# 文字分行
|
|
||||||
|
text_str = text_str.replace("\t", " ")
|
||||||
|
|
||||||
# 分行
|
# 分行
|
||||||
lines = text_str.split('\n')
|
lines = text_str.split('\n')
|
||||||
@@ -92,7 +93,7 @@ def text_to_image(text_str, save_as="temp.png", width=800):
|
|||||||
# 计算并分割
|
# 计算并分割
|
||||||
final_lines = []
|
final_lines = []
|
||||||
|
|
||||||
text_width = width-40
|
text_width = width-80
|
||||||
for line in lines:
|
for line in lines:
|
||||||
# 如果长了就分割
|
# 如果长了就分割
|
||||||
line_width = text_render_font.getlength(line)
|
line_width = text_render_font.getlength(line)
|
||||||
@@ -122,14 +123,14 @@ def text_to_image(text_str, save_as="temp.png", width=800):
|
|||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
# 准备画布
|
# 准备画布
|
||||||
img = Image.new('RGBA', (width, max(280, len(final_lines) * 35 + 35)), (255, 255, 255, 255))
|
img = Image.new('RGBA', (width, max(280, len(final_lines) * 35 + 45)), (255, 255, 255, 255))
|
||||||
draw = ImageDraw.Draw(img, mode='RGBA')
|
draw = ImageDraw.Draw(img, mode='RGBA')
|
||||||
|
|
||||||
|
|
||||||
# 绘制正文
|
# 绘制正文
|
||||||
line_number = 0
|
line_number = 0
|
||||||
offset_x = 20
|
offset_x = 20
|
||||||
offset_y = 20
|
offset_y = 30
|
||||||
for final_line in final_lines:
|
for final_line in final_lines:
|
||||||
draw.text((offset_x, offset_y + 35 * line_number), final_line, fill=(0, 0, 0), font=text_render_font)
|
draw.text((offset_x, offset_y + 35 * line_number), final_line, fill=(0, 0, 0), font=text_render_font)
|
||||||
# 遍历此行,检查是否有emoji
|
# 遍历此行,检查是否有emoji
|
||||||
|
|||||||
Reference in New Issue
Block a user