Cosmos01 / Blue_Archive_HoshinoBot

Blue Archive 碧蓝档案(日服\国服\国际服通用)图形化活动日历、模拟抽卡、官推转发、攻略图获取插件, 适用于 HoshinoBot v2.
GNU General Public License v3.0
100 stars 4 forks source link

在Pillow版本升级后(安装头像表情包插件后),ba日历报错的解决方法 #15

Closed FanJZSS closed 9 months ago

FanJZSS commented 9 months ago

刚才发现定时推送的日历没推送,查了历史记录发现这两天定时发送的ba日历没有推送,看了一下HoshinoBot的error.log有如下报错

[2023-12-12 16:39:50,898 ba_calendar] ERROR: <class 'AttributeError'> occured when start_scheduled handling message -1120049348.
[2023-12-12 16:39:50,898 ba_calendar] ERROR: 'ImageDraw' object has no attribute 'textsize'
Traceback (most recent call last):
  File "C:\Users\Fan\Desktop\HoshinoBot\hoshino\msghandler.py", line 23, in handle_message
    await service_func.func(bot, event)
  File "C:\Users\Fan\Desktop\HoshinoBot\hoshino\modules\Blue_Archive_HoshinoBot\calendar.py", line 113, in start_scheduled
    img = await generate_day_schedule(server)
  File "C:\Users\Fan\Desktop\HoshinoBot\hoshino\modules\Blue_Archive_HoshinoBot\ba_calendar\generate.py", line 30, in generate_day_schedule
    draw_title(im, 0, title, ba_now.strftime('%Y/%m/%d'), '正在进行')
  File "C:\Users\Fan\Desktop\HoshinoBot\hoshino\modules\Blue_Archive_HoshinoBot\ba_calendar\draw.py", line 76, in draw_title
    draw_text(im, x, y, width, height, middle, 0, color[0]['front'])
  File "C:\Users\Fan\Desktop\HoshinoBot\hoshino\modules\Blue_Archive_HoshinoBot\ba_calendar\draw.py", line 35, in draw_text
    tw, th = draw.textsize(text, font=font)
AttributeError: 'ImageDraw' object has no attribute 'textsize'

想起最近加了个头像表情包生成器的插件,排查后发现是该插件升级了Pillow版本,由初始的9.1.1升到了10.1.0导致的错误,问了一下GPT有如下解决方法

Blue_Archive_HoshinoBot/ba_calendar/draw.py文件

第35行

tw, th = draw.textsize(text, font=font)

改为

bbox = draw.textbbox((x, y), text, font=font)
tw, th = bbox[2] - bbox[0], bbox[3] - bbox[1]

目前能正常发ba日历,头像表情包生成器也能正常使用,副作用未知

Cosmos01 commented 9 months ago

嗯,应该是没啥问题,我最近写总力排名功能刚好也遇到这问题了,下次更新我改改吧。 新版本好像是用textlength替代了,但是低版本不支持这个,用你这个方法应该更好点。 我的头像表情包插件在9.1.1好像是正常运行的,直接降版本应该也是可以的。