Skip to content

Commit

Permalink
fixed: 修复了使用网络图片时,同步下载失败问题
Browse files Browse the repository at this point in the history
perf: 更新了最新的pillow
perf: 更新了数据库
  • Loading branch information
fireinsect committed Jun 12, 2024
1 parent 5770796 commit 202ada7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
12 changes: 10 additions & 2 deletions nonebot_plugin_ocgbot_v2/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import nonebot
from nonebot import get_plugin_config
from pydantic import BaseModel
from yarl import URL


global_config = nonebot.get_driver().config
class Config(BaseModel):
static_path: str = ""
use_web_pic: bool = False

bison_outer_url: str = ""
@property
def outer_url(self) -> URL:
if self.bison_outer_url:
return URL(self.bison_outer_url)
else:
return URL(f"http://localhost:{global_config.port}/bison/")

config = get_plugin_config(Config)
21 changes: 13 additions & 8 deletions nonebot_plugin_ocgbot_v2/libraries/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def text_to_image(text):
text_list = text.split('\n')
max_width = 0
for text in text_list:
w, h = font.getsize(text)
w, h = getsize(font, text)
max_width = max(max_width, w)
wa = max_width + padding * 2
ha = h * len(text_list) + margin * (len(text_list) - 1) + padding * 2
Expand All @@ -48,16 +48,16 @@ def text_to_image2(text, page_text):
padding = 30
margin = 33
text_list = text.split('\n')
max_width = font.getsize(page_text)[0]
max_width = font.getbbox(page_text)[0]
for text in text_list:
w, h = font.getsize(text)
w, h = getsize(font, text)
max_width = max(max_width, w)
wa = max_width + padding * 2 + 100
ha = h * len(text_list) + h + margin * (len(text_list)) + padding * 2
# i = Image.new('RGB', (wa, ha), color=(255, 255, 255))
i = background
change = max(ha / i.height, wa / i.width)
i = i.resize((int(i.width * change), int(i.height * change)), Image.ANTIALIAS)
i = i.resize((int(i.width * change), int(i.height * change)), Image.LANCZOS)
i = i.crop(
(int(i.width / 2 - wa / 2), int(i.height / 2 - ha / 2), int(i.width / 2 + wa / 2), int(i.height / 2 + ha / 2)))
draw = ImageDraw.Draw(i)
Expand All @@ -76,17 +76,17 @@ def text_to_image_with_back(text, page_text, title):
margin = 33
text_list = text.split('\n')
max_width = 0
title_w, title_h = font_title.getsize(title)
title_w, title_h = getsize(font_title, title)
for text in text_list:
w, h = font.getsize(text)
w, h = getsize(font, text)
max_width = max(max_width, w)
max_width = max(max_width, font.getsize(page_text)[0])
max_width = max(max_width, font.getlength(page_text))
max_width = max(max_width, title_w)
wa = max_width + padding * 2 + 100
ha = h * len(text_list) + h + margin * (len(text_list)) + padding * 2 + title_h + int(title_h * 0.8)
i = background
change = max(ha / i.height, wa / i.width)
i = i.resize((int(i.width * change), int(i.height * change)), Image.ANTIALIAS)
i = i.resize((int(i.width * change), int(i.height * change)), Image.LANCZOS)
i = i.crop(
(int(i.width / 2 - wa / 2), int(i.height / 2 - ha / 2), int(i.width / 2 + wa / 2), int(i.height / 2 + ha / 2)))
draw = ImageDraw.Draw(i)
Expand All @@ -104,3 +104,8 @@ def image_to_base64(img, format='PNG'):
byte_data = output_buffer.getvalue()
base64_str = base64.b64encode(byte_data)
return base64_str


def getsize(font, text):
left, top, right, bottom = font.getbbox(text)
return right - left, bottom - top
17 changes: 9 additions & 8 deletions nonebot_plugin_ocgbot_v2/libraries/sendAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def send(js, bot, event, func, num=0):
num = num - 1
pics_url = static_url / (str(
js.cards[num].cardId) + '.jpg')
if img_exist(pics_url):
if await img_exist(pics_url):
messageListAppend(js, pics_url, num, msg_list)
else:
jsex = CardResult()
Expand All @@ -82,7 +82,7 @@ async def send(js, bot, event, func, num=0):
jsex.nowNum = js.nowNum
jsex.cards = [js.cards[num]]
await messageListCreate(jsex, msg_list)
elif js.amount == 1 and img_exist(pics_url):
elif js.amount == 1 and await img_exist(pics_url):
messageListAppend(js, pics_url, num, msg_list)
else:
messageListCreate(js, msg_list)
Expand Down Expand Up @@ -113,7 +113,7 @@ async def send2(js, func, num=0):
num = num - 1
pics_url = static_url / (str(
js.cards[num].cardId) + '.jpg')
if img_exist(pics_url):
if await img_exist(pics_url):
await func.finish(getAllMessage(js, pics_url, num))
else:
jsex = CardResult()
Expand All @@ -122,7 +122,7 @@ async def send2(js, func, num=0):
jsex.nowNum = js.nowNum
jsex.cards = [js.cards[num]]
await send_cards_byCard(jsex, func)
elif js.amount == 1 and img_exist(pics_url):
elif js.amount == 1 and await img_exist(pics_url):
await func.finish(getAllMessage(js, pics_url, num))
else:
await send_cards_byCard(js, func)
Expand Down Expand Up @@ -165,7 +165,7 @@ def messageListAppend(js, url, num, msg_list):
{
# "file": f"base64://{str(image_to_base64(Image.open('src/static/pics/' + str(js['data'][
# 'cards'][0]['cardId']) + '.jpg')), encoding='utf-8')}"
"file": f"base64://{str(image_to_base64(img.resize((int(img.size[0] * PANTOGRAPH), int(img.size[1] * PANTOGRAPH)), Image.ANTIALIAS)), encoding='utf-8')}"}
"file": f"base64://{str(image_to_base64(img.resize((int(img.size[0] * PANTOGRAPH), int(img.size[1] * PANTOGRAPH)), Image.LANCZOS)), encoding='utf-8')}"}
)
]))
msg_list.append(Message([
Expand Down Expand Up @@ -223,7 +223,7 @@ def getAllMessage(js, url, num):
{
# "file": f"base64://{str(image_to_base64(Image.open('src/static/pics/' + str(js['data'][
# 'cards'][0]['cardId']) + '.jpg')), encoding='utf-8')}"
"file": f"base64://{str(image_to_base64(img.resize((int(img.size[0] * PANTOGRAPH), int(img.size[1] * PANTOGRAPH)), Image.ANTIALIAS)), encoding='utf-8')}"
"file": f"base64://{str(image_to_base64(img.resize((int(img.size[0] * PANTOGRAPH), int(img.size[1] * PANTOGRAPH)), Image.LANCZOS)), encoding='utf-8')}"
}
),
MessageSegment(
Expand Down Expand Up @@ -259,11 +259,12 @@ def getPicOnlyMessage(js, num, url):

# =========判断============
# 判断图片是否存在
def img_exist(url):
async def img_exist(url):
print(url)
if url.exists():
return True
else:
return asyncio.run(downLoadFromWeb(url))
return await downLoadFromWeb(url)


# =====================
Expand Down
Binary file modified nonebot_plugin_ocgbot_v2/static/cdb/cards.cdb
Binary file not shown.
Binary file modified nonebot_plugin_ocgbot_v2/static/cdb/pre-release.cdb
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ nonebot2 = ">=2.0.0"
nonebot-adapter-onebot = ">=2.2.3"
requests_html = ">=0.10.0"
lxml-html-clean = ">=0.1.1"
Pillow = "<=9.5.0"
Pillow = ">=9.5.0"
httpx = ">=0.20.0,<1.0.0"
numpy = ">=1.23.0"
matplotlib = ">=3.7.1"
Expand Down

0 comments on commit 202ada7

Please sign in to comment.