Skip to content

Commit

Permalink
✨ 新增角色材料的图片版 (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
KimigaiiWuyi committed Apr 21, 2023
1 parent b3c495d commit df72b97
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 131 deletions.
6 changes: 3 additions & 3 deletions GenshinUID/genshinuid_collection/draw_collection_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
green_color = (74, 189, 119)

max_data = {
'成就': 945,
'华丽的宝箱': 192,
'成就': 950,
'华丽的宝箱': 193,
'珍贵的宝箱': 510,
'精致的宝箱': 1639,
'精致的宝箱': 1640,
'普通的宝箱': 2690,
'奇馈宝箱': 161,
'解锁传送点': 304,
Expand Down
2 changes: 1 addition & 1 deletion GenshinUID/genshinuid_gachalog/draw_gachalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
]

UP_LIST = {
'刻晴': [(2022, 8, 24, 11, 0, 0), (2022, 9, 9, 17, 59, 59)],
'刻晴': [(2021, 2, 17, 18, 0, 0), (2021, 3, 2, 15, 59, 59)],
'提纳里': [(2022, 8, 24, 11, 0, 0), (2022, 9, 9, 17, 59, 59)],
'迪希雅': [(2023, 3, 1, 11, 0, 0), (2023, 3, 21, 17, 59, 59)],
}
Expand Down
6 changes: 5 additions & 1 deletion GenshinUID/genshinuid_wikitext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from gsuid_core.segment import MessageSegment

from .get_foods_pic import get_foods_wiki_img
from .get_cost_pic import get_char_cost_wiki_img
from .get_weapons_pic import get_weapons_wiki_img
from ..genshinuid_config.gs_config import gsconfig
from .get_artifacts_pic import get_artifacts_wiki_img
Expand Down Expand Up @@ -97,7 +98,10 @@ async def send_char(bot: Bot, ev: Event):
@sv_wiki_text.on_prefix(('角色材料'))
async def send_char_cost(bot: Bot, ev: Event):
name = await alias_to_char_name(ev.text)
im = await char_costs_wiki(name)
if gsconfig.get_config('PicWiki').data:
im = await get_char_cost_wiki_img(name)
else:
im = await char_costs_wiki(name)
await bot.send(im)


Expand Down
2 changes: 1 addition & 1 deletion GenshinUID/genshinuid_wikitext/get_artifacts_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def draw_artifacts_wiki_img(data: Artifact) -> bytes:
result_img.save(
WIKI_REL_PATH / '{}.jpg'.format(data['name']),
format='JPEG',
quality=95,
quality=96,
subsampling=0,
)
return await convert_img(result_img)
4 changes: 2 additions & 2 deletions GenshinUID/genshinuid_wikitext/get_constellation_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def draw_constellation_wiki_img(
img = await get_simple_bg(600, 280 + y, bg)
img_draw = ImageDraw.Draw(img)

desc = await str_lenth(char_data['description'], 18, 341)
desc = await str_lenth(char_data['description'], 18, 350)

avatar_id = await name_to_avatar_id(data['name'])
char_img = Image.open(CHAR_PATH / f'{avatar_id}.png')
Expand Down Expand Up @@ -205,7 +205,7 @@ async def draw_constellation_wiki_img(
img.save(
CONSTELLATION_PATH / '{}.jpg'.format(data['name']),
format='JPEG',
quality=95,
quality=96,
subsampling=0,
)
return await convert_img(img)
192 changes: 192 additions & 0 deletions GenshinUID/genshinuid_wikitext/get_cost_pic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
from typing import Dict, List, Tuple, Union

import aiofiles
from PIL import Image, ImageDraw

from .path import TEXT_PATH
from ..utils.colors import white_color
from ..utils.error_reply import get_error
from ..utils.get_assets import get_assets_from_ambr
from ..utils.map.name_covert import name_to_avatar_id
from ..utils.image.convert import str_lenth, convert_img
from ..gsuid_utils.api.minigg.models import Character, CharacterTalents
from ..utils.resource.RESOURCE_PATH import CHAR_PATH, WIKI_COST_CHAR_PATH
from ..gsuid_utils.api.minigg.request import (
get_others_info,
get_talent_info,
get_character_info,
)
from ..utils.image.image_tools import (
get_star_png,
get_simple_bg,
get_unknown_png,
draw_pic_with_ring,
)
from ..utils.fonts.genshin_fonts import (
gs_font_24,
gs_font_26,
gs_font_30,
gs_font_36,
gs_font_44,
)


async def get_char_cost_wiki_img(name: str) -> Union[str, bytes]:
data = await get_character_info(name)
talent_data = await get_talent_info(name)
if isinstance(data, int):
return get_error(data)
elif isinstance(data, List):
return get_error(-400)
elif isinstance(talent_data, int):
return get_error(talent_data)
else:
char_name = talent_data['name']
path = WIKI_COST_CHAR_PATH / f'{char_name}.jpg'
if path.exists():
async with aiofiles.open(path, 'rb') as f:
return await f.read()
img = await draw_char_cost_img(data, talent_data)
return img


async def draw_single_cost(title: str, data: Dict) -> Tuple[Image.Image, str]:
img = Image.new('RGBA', (900, 450))
img_draw = ImageDraw.Draw(img)
wiki_cost_bg = Image.open(TEXT_PATH / 'wiki_weapon_cost.png').resize(
(900, 255)
)
cost_bg = Image.open(TEXT_PATH / 'cost_bg.png')
img.paste(cost_bg, (0, 0), cost_bg)
img.paste(wiki_cost_bg, (0, 15), wiki_cost_bg)
img.paste(wiki_cost_bg, (0, 180), wiki_cost_bg)

wiki_cost_tag = Image.open(TEXT_PATH / 'cost_tag.png').resize((75, 75))
img.paste(wiki_cost_tag, (65, -20), wiki_cost_tag)
img_draw.text((130, 22), title, white_color, gs_font_36, 'lm')
cost_pos = ''
for index, cost_name in enumerate(data):
material = await get_others_info('materials', cost_name)
if isinstance(material, int):
cost_pic = get_unknown_png()
else:
name_icon = material['images']['nameicon']
_cost_pic = await get_assets_from_ambr(name_icon)
if _cost_pic is None:
cost_pic = get_unknown_png().resize((96, 96))
else:
cost_pic = _cost_pic.resize((96, 96))

if (
material['materialtype'] == '武器突破素材'
or material['materialtype'] == '角色天赋素材'
) and 'daysofweek' in material:
pos = material['dropdomain']
days = material['daysofweek']
if '周日' in days:
days.remove('周日')
cost_pos = f'{pos} - {"/".join(days)}'
else:
cost_pos = ''

t = 150 * (index % 5)
y = 165 * (index // 5)
tent_x = 34
tent_y = 23
img.paste(cost_pic, (67 + tent_x + t, 61 + tent_y + y), cost_pic)
val = str(data[cost_name])
'''
img_draw.text(
(114 + tent_x + t, 45 + tent_y + y),
cost_pos,
white_color,
gs_font_20,
'mm',
)
'''
img_draw.text(
(114 + tent_x + t, 175 + tent_y + y),
val,
white_color,
gs_font_26,
'mm',
)
return img, cost_pos


async def draw_char_cost_img(data: Character, talent_data: CharacterTalents):
talent_costs = {}
talent_cost = talent_data['costs']
for i in talent_cost.values():
for j in i: # type:ignore
if j['name'] not in talent_costs:
talent_costs[j['name']] = j['count']
else:
talent_costs[j['name']] = talent_costs[j['name']] + j['count']

ascend_costs = {}
char_cost = data['costs']
for i in range(1, 7):
for j in char_cost[f'ascend{i}']:
if j['name'] not in ascend_costs:
ascend_costs[j['name']] = j['count']
else:
ascend_costs[j['name']] = ascend_costs[j['name']] + j['count']

bg = Image.open(TEXT_PATH / 'wiki_weapon_bg.jpg')
img = await get_simple_bg(900, 1800, bg)
img_draw = ImageDraw.Draw(img)

desc = await str_lenth(data['description'], 18, 341)

avatar_id = await name_to_avatar_id(data['name'])
char_img = Image.open(CHAR_PATH / f'{avatar_id}.png')
icon = await draw_pic_with_ring(char_img, 222)
img.paste(icon, (80, 90), icon)

title = data["title"].replace('「', '').replace('」', '')
img_draw.text((336, 230), desc, (230, 230, 230), gs_font_24)
img_draw.text(
(400, 161),
f'{title}·{data["name"]}',
(255, 255, 255),
gs_font_44,
'lm',
)

star_pic = get_star_png(data['rarity'])
element_pic_path = TEXT_PATH / f'{data["element"]}.png'
if element_pic_path.exists():
element_pic = Image.open(element_pic_path).resize((54, 54))
else:
element_pic = get_unknown_png().resize((54, 54))
img.paste(element_pic, (330, 130), element_pic)
img.paste(star_pic, (335, 188), star_pic)

talent_costs = dict(sorted(talent_costs.items(), key=lambda x: len(x[0])))
ascend_costs = dict(sorted(ascend_costs.items(), key=lambda x: len(x[0])))

cost1, pos1 = await draw_single_cost('突破素材消耗', ascend_costs)
cost2, pos2 = await draw_single_cost(
'天赋素材消耗', {i: talent_costs[i] * 3 for i in talent_costs}
)
cost3, pos3 = await draw_single_cost('天赋素材消耗 (一份)', talent_costs)

pos = max(pos1, pos2, pos3, key=len)
cost_title = Image.open(TEXT_PATH / 'cost_title.png')
img.paste(cost_title, (0, 338), cost_title)

img_draw.text((450, 383), pos, (255, 255, 255), gs_font_30, 'mm')

img.paste(cost1, (0, 360 + 100), cost1)
img.paste(cost2, (0, 705 + 100 + 100), cost2)
img.paste(cost3, (0, 1050 + 100 + 100 + 100), cost3)

img = img.convert('RGB')
img.save(
WIKI_COST_CHAR_PATH / '{}.jpg'.format(data['name']),
format='JPEG',
quality=97,
subsampling=0,
)
return await convert_img(img)
2 changes: 1 addition & 1 deletion GenshinUID/genshinuid_wikitext/get_foods_pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def draw_foods_wiki_img(data: Food):
img.save(
WIKI_FOOD_PATH / '{}.jpg'.format(data['name']),
format='JPEG',
quality=95,
quality=96,
subsampling=0,
)
return await convert_img(img)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions GenshinUID/utils/resource/RESOURCE_PATH.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
WIKI_TALENT_PATH = WIKI_PATH / 'talent'
WIKI_ENEMY_PATH = WIKI_PATH / 'enemy'
WIKI_CHAR_PATH = WIKI_PATH / 'char'
WIKI_COST_CHAR_PATH = WIKI_PATH / 'cost_char'
WIKI_COST_WEAPON_PATH = WIKI_PATH / 'cost_weapon'
TEXT2D_PATH = Path(__file__).parent / 'texture2d'

PLAYER_PATH = MAIN_PATH / 'players'
Expand Down Expand Up @@ -64,6 +66,8 @@ def init_dir():
WIKI_TALENT_PATH,
WIKI_ENEMY_PATH,
WIKI_CHAR_PATH,
WIKI_COST_CHAR_PATH,
WIKI_COST_WEAPON_PATH,
]:
i.mkdir(parents=True, exist_ok=True)

Expand Down
Loading

0 comments on commit df72b97

Please sign in to comment.