LittlePaimon/Paimon_Info/draw_month_info.py

115 lines
5.3 KiB
Python
Raw Normal View History

2022-04-30 14:13:39 +08:00
import os
2022-05-20 18:44:18 +08:00
import random
2022-04-30 14:13:39 +08:00
import matplotlib.pyplot as plt
2022-05-20 18:44:18 +08:00
from PIL import Image, ImageDraw, ImageFont
from utils.message_util import MessageBuild
2022-04-30 14:13:39 +08:00
res_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'res')
def get_font(size):
return ImageFont.truetype(os.path.join(res_path, 'msyh.ttc'), size)
def get_font_bd(size):
return ImageFont.truetype(os.path.join(res_path, 'msyhbd.ttc'), size)
async def get_box(t, num):
box = Image.open(os.path.join(res_path, 'monthinfo', 'box.png')).convert('RGBA')
img = Image.open(os.path.join(res_path, 'monthinfo', f'{t}.png')).convert('RGBA')
box.alpha_composite(img, (11, 11))
box_draw = ImageDraw.Draw(box)
box_draw.text((83, 18), f'{t}', font=get_font(25), fill='black')
if t == '原石':
box_draw.text((152, 18), f'{num}|{int(num / 160)}', font=get_font(25), fill='#9E8E7B')
else:
box_draw.text((152, 18), f'{num}', font=get_font(25), fill='#9E8E7B')
return box
async def draw_ring(per, colors):
2022-04-30 14:13:39 +08:00
plt.pie(per, startangle=90, colors=colors)
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(per,
wedgeprops={'width': 0.29},
startangle=90,
colors=colors)
plt.savefig('temp.png', transparent=True)
img = Image.open('temp.png').resize((378, 378)).convert('RGBA')
os.remove('temp.png')
return img
2022-04-30 14:13:39 +08:00
async def draw_monthinfo_card(data):
if not data:
return '数据出错'
if data['retcode'] == 10102:
return '这uid没有在米游社公开信息哦,请到 个人主页-管理 中打开'
elif data['retcode'] == 10104:
return 'uid有误哦检查一下或再手动输入一次uid吧'
elif data['retcode'] == -2:
return '这个月份的信息查不到噢!'
elif data['retcode'] == -110:
return '这个uid没有绑定cookie哦!'
elif data['retcode'] != 0:
return f'派蒙获取数据失败了,获取状态:\n{data["message"]},{data["retcode"]}'
data = data['data']
bg_img = Image.open(os.path.join(res_path, 'monthinfo', 'bg.png')).convert('RGBA')
bg_draw = ImageDraw.Draw(bg_img)
line = Image.open(os.path.join(res_path, 'monthinfo', 'line.png')).convert('RGBA')
# 顶标题
bg_draw.text((60, 42), f'旅行者{data["data_month"]}月札记', font=get_font_bd(30), fill='#27384C')
bg_draw.text((300, 52), f'{data["nickname"]} {data["uid"]}', font=get_font(21), fill='#27384C')
bg_img.alpha_composite(line, (64, 95))
# 月获取
bg_draw.text((60, 110), '当月共获取:', font=get_font_bd(25), fill='#27384C')
bg_img.alpha_composite(await get_box('原石', data['month_data']['current_primogems']), (40, 150))
bg_img.alpha_composite(await get_box('摩拉', data['month_data']['current_mora']), (40, 210))
# 日获取
bg_draw.text((60, 288), '今日已获取:', font=get_font_bd(25), fill='#27384C')
bg_img.alpha_composite(await get_box('原石', data['day_data']['current_primogems']), (40, 328))
bg_img.alpha_composite(await get_box('摩拉', data['day_data']['current_mora']), (40, 388))
# 表情
emoticon1 = Image.open(os.path.join(res_path, 'emoticons', random.choice(os.listdir(os.path.join(res_path, 'emoticons'))))).convert('RGBA')
2022-04-30 14:13:39 +08:00
bg_img.alpha_composite(emoticon1, (360, 140))
emoticon2 = Image.open(os.path.join(res_path, 'emoticons', random.choice(os.listdir(os.path.join(res_path, 'emoticons'))))).convert('RGBA')
2022-04-30 14:13:39 +08:00
bg_img.alpha_composite(emoticon2, (360, 317))
bg_img.alpha_composite(line, (64, 480))
# 圆环比例图
bg_draw.text((60, 495), '原石收入组成:', font=get_font_bd(25), fill='#27384C')
circle = Image.open(os.path.join(res_path, 'monthinfo', 'circle.png')).convert('RGBA')
bg_img.alpha_composite(circle, (50, 550))
color = {'每日活跃': '#BD9A5A', '深境螺旋': '#739970', '活动奖励': '#5A7EA0', '邮件奖励': '#7A6CA7', '冒险奖励': '#D56564',
'任务奖励': '#70B1B3', '其他': '#73A8C7'}
per_list = [x['percent'] for x in data['month_data']['group_by']]
name_list = [x['action'] for x in data['month_data']['group_by']]
num_list = [x['num'] for x in data['month_data']['group_by']]
color_list = [color[x] for x in name_list]
bg_img.alpha_composite(await draw_ring(per_list, color_list), (-12, 489))
2022-04-30 14:13:39 +08:00
# 百分比描述
h = 550
for name in name_list:
bg_draw.rectangle(((330, h), (340, h + 10)), fill=color[name])
bg_draw.text((345, h - 6), name, font=get_font(17), fill='#27384C')
bg_draw.text((430, h - 6), str(num_list[name_list.index(name)]), font=get_font(17), fill='#27384C')
bg_draw.text((480, h - 6), str(per_list[name_list.index(name)]) + '%', font=get_font(17), fill='#27384C')
h += 40
if data['month_data']['primogems_rate'] < 0:
ysstr = f'少了{-data["month_data"]["primogems_rate"]}%'
else:
ysstr = f'多了{data["month_data"]["primogems_rate"]}%'
if data['month_data']['mora_rate'] < 0:
mlstr = f'少了{-data["month_data"]["mora_rate"]}%'
else:
mlstr = f'多了{data["month_data"]["mora_rate"]}%'
bg_img.alpha_composite(line, (64, 840))
bg_draw.text((49, 857), f'本月相比上个月,原石{ysstr},摩拉{mlstr}', font=get_font(23), fill='#27384C')
bg_draw.text((167, 900), 'Created by LittlePaimon', font=get_font(21), fill='#27384C')
2022-04-30 14:13:39 +08:00
2022-05-20 18:44:18 +08:00
return MessageBuild.Image(bg_img, quality=70)