fix:优化表情包ascii问题

This commit is contained in:
SengokuCola
2025-06-16 13:47:25 +08:00
parent 3951a3a39a
commit 2cbd9745d2
3 changed files with 26 additions and 2 deletions

View File

@@ -74,6 +74,9 @@ class MaiEmoji:
# 计算哈希值 # 计算哈希值
logger.debug(f"[初始化] 正在解码Base64并计算哈希: {self.filename}") logger.debug(f"[初始化] 正在解码Base64并计算哈希: {self.filename}")
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
self.hash = hashlib.md5(image_bytes).hexdigest() self.hash = hashlib.md5(image_bytes).hexdigest()
logger.debug(f"[初始化] 哈希计算成功: {self.hash}") logger.debug(f"[初始化] 哈希计算成功: {self.hash}")
@@ -841,6 +844,9 @@ class EmojiManager:
""" """
try: try:
# 解码图片并获取格式 # 解码图片并获取格式
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
image_format = Image.open(io.BytesIO(image_bytes)).format.lower() image_format = Image.open(io.BytesIO(image_bytes)).format.lower()

View File

@@ -98,6 +98,9 @@ class ImageManager:
"""获取表情包描述,带查重和保存功能""" """获取表情包描述,带查重和保存功能"""
try: try:
# 计算图片哈希 # 计算图片哈希
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
image_hash = hashlib.md5(image_bytes).hexdigest() image_hash = hashlib.md5(image_bytes).hexdigest()
image_format = Image.open(io.BytesIO(image_bytes)).format.lower() image_format = Image.open(io.BytesIO(image_bytes)).format.lower()
@@ -113,10 +116,10 @@ class ImageManager:
if image_base64_processed is None: if image_base64_processed is None:
logger.warning("GIF转换失败无法获取描述") logger.warning("GIF转换失败无法获取描述")
return "[表情包(GIF处理失败)]" return "[表情包(GIF处理失败)]"
prompt = "这是一个动态图表情包每一张图代表了动态图的某一帧黑色背景代表透明使用1-2个词描述一下表情包表达的情感和内容简短一些" prompt = "这是一个动态图表情包每一张图代表了动态图的某一帧黑色背景代表透明使用1-2个词描述一下表情包表达的情感和内容简短一些,输出一段平文本"
description, _ = await self._llm.generate_response_for_image(prompt, image_base64_processed, "jpg") description, _ = await self._llm.generate_response_for_image(prompt, image_base64_processed, "jpg")
else: else:
prompt = "这是一个表情包,请用使用几个词描述一下表情包所表达的情感和内容,简短一些" prompt = "这是一个表情包,请用使用几个词描述一下表情包所表达的情感和内容,简短一些,输出一段平文本"
description, _ = await self._llm.generate_response_for_image(prompt, image_base64, image_format) description, _ = await self._llm.generate_response_for_image(prompt, image_base64, image_format)
if description is None: if description is None:
@@ -175,6 +178,9 @@ class ImageManager:
"""获取普通图片描述,带查重和保存功能""" """获取普通图片描述,带查重和保存功能"""
try: try:
# 计算图片哈希 # 计算图片哈希
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
image_hash = hashlib.md5(image_bytes).hexdigest() image_hash = hashlib.md5(image_bytes).hexdigest()
image_format = Image.open(io.BytesIO(image_bytes)).format.lower() image_format = Image.open(io.BytesIO(image_bytes)).format.lower()
@@ -255,6 +261,9 @@ class ImageManager:
Optional[str]: 拼接后的JPG图像的base64编码字符串, 或者在失败时返回None Optional[str]: 拼接后的JPG图像的base64编码字符串, 或者在失败时返回None
""" """
try: try:
# 确保base64字符串只包含ASCII字符
if isinstance(gif_base64, str):
gif_base64 = gif_base64.encode('ascii', errors='ignore').decode('ascii')
# 解码base64 # 解码base64
gif_data = base64.b64decode(gif_base64) gif_data = base64.b64decode(gif_base64)
gif = Image.open(io.BytesIO(gif_data)) gif = Image.open(io.BytesIO(gif_data))
@@ -374,6 +383,9 @@ class ImageManager:
try: try:
# 生成图片ID # 生成图片ID
# 计算图片哈希 # 计算图片哈希
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
image_hash = hashlib.md5(image_bytes).hexdigest() image_hash = hashlib.md5(image_bytes).hexdigest()
@@ -444,6 +456,9 @@ class ImageManager:
""" """
try: try:
# 计算图片哈希 # 计算图片哈希
# 确保base64字符串只包含ASCII字符
if isinstance(image_base64, str):
image_base64 = image_base64.encode('ascii', errors='ignore').decode('ascii')
image_bytes = base64.b64decode(image_base64) image_bytes = base64.b64decode(image_base64)
image_hash = hashlib.md5(image_bytes).hexdigest() image_hash = hashlib.md5(image_bytes).hexdigest()

View File

@@ -836,6 +836,9 @@ def compress_base64_image_by_scale(base64_data: str, target_size: int = 0.8 * 10
""" """
try: try:
# 将base64转换为字节数据 # 将base64转换为字节数据
# 确保base64字符串只包含ASCII字符
if isinstance(base64_data, str):
base64_data = base64_data.encode('ascii', errors='ignore').decode('ascii')
image_data = base64.b64decode(base64_data) image_data = base64.b64decode(base64_data)
# 如果已经小于目标大小,直接返回原图 # 如果已经小于目标大小,直接返回原图