首页 > 解决方案 > Discord.py 等级卡添加文字(枕头)

问题描述

所以我正在为我自己的不和谐服务器制作聊天!排名命令,我用 mongodb 完成了数据库,我也按照教程完成了排名卡格式,但我想添加一个总 xp 字段,代码中的教程没有,我是个带枕头的菜鸟。我当前的代码:

async def make_rank_image(self, member: discord.Member, rank, level, xp, final_xp,status,Totalxp):#Totalxp is unused and will be used in the totalxp text field
        user_avatar_image = str(member.avatar_url_as(format='png', size=512))
        async with aiohttp.ClientSession() as session:
            async with session.get(user_avatar_image) as resp:
                avatar_bytes = io.BytesIO(await resp.read())

        img = Image.new('RGB', (1000, 240))
        logo = Image.open(avatar_bytes).resize((200, 200))
        # Stack overflow helps :)
        bigsize = (logo.size[0] * 3, logo.size[1] * 3)
        mask = Image.new('L', bigsize, 0)
        draw = ImageDraw.Draw(mask)
        draw.ellipse((0, 0) + bigsize, fill=255)
        mask = mask.resize(logo.size, Image.ANTIALIAS)
        logo.putalpha(mask)
        ##############################
        img.paste(logo, (20, 20), mask=logo)

        # Black Circle
        draw = ImageDraw.Draw(img, 'RGB')
        draw.ellipse((152, 152, 208, 208), fill='#000')

        # Placing offline or Online Status
        if str(status) == "online":
            draw.ellipse((155, 155, 205, 205), fill='#3BA55B')
        elif str(status) == "idle":
            draw.ellipse((155, 155, 205, 205), fill='#F9A61A')
        elif str(status) == "dnd":
            draw.ellipse((155, 155, 205, 205), fill='#EC4245')
        else:
            draw.ellipse((155, 155, 205, 205), fill='#737F8D')
        ##################################

        # Working with fonts
        big_font = ImageFont.FreeTypeFont('ABeeZee-Regular.otf', 60)
        medium_font = ImageFont.FreeTypeFont('ABeeZee-Regular.otf', 40)
        small_font = ImageFont.FreeTypeFont('ABeeZee-Regular.otf', 30)
        extra_small_front=ImageFont.FreeTypeFont('ABeeZee-Regular.otf',10)
        # Placing Level text (right-upper part)
        text_size = draw.textsize(f"{level}", font=big_font)
        offset_x = 1000 - 15 - text_size[0]
        offset_y = 5
        draw.text((offset_x, offset_y), f"{level}", font=big_font, fill="#11ebf2")
        text_size = draw.textsize('LEVEL', font=small_font)

        offset_x -= 5 + text_size[0]
        offset_y = 35
        draw.text((offset_x, offset_y), "LEVEL", font=small_font, fill="#11ebf2")
        #totalxp field
        How do you do this?????????



        # Placing Rank Text (right upper part)
        text_size = draw.textsize(f"#{rank}", font=big_font)
        offset_x -= 15 + text_size[0]
        offset_y = 8
        draw.text((offset_x, offset_y), f"#{rank}", font=big_font, fill="#fff")

        text_size = draw.textsize("RANK", font=small_font)
        offset_x -= 5 + text_size[0]
        offset_y = 35
        draw.text((offset_x, offset_y), "RANK", font=small_font, fill="#fff")

        # Placing Progress Bar
        # Background Bar
        bar_offset_x = logo.size[0] + 20 + 100
        bar_offset_y = 160
        bar_offset_x_1 = 1000 - 50
        bar_offset_y_1 = 200
        circle_size = bar_offset_y_1 - bar_offset_y

        # Progress bar rect greyier one
        draw.rectangle((bar_offset_x, bar_offset_y, bar_offset_x_1, bar_offset_y_1), fill="#727175")
        # Placing circle in progress bar

        # Left circle
        draw.ellipse((bar_offset_x - circle_size // 2, bar_offset_y, bar_offset_x + circle_size // 2,
                      bar_offset_y + circle_size), fill="#727175")

        # Right Circle
        draw.ellipse(
            (bar_offset_x_1 - circle_size // 2, bar_offset_y, bar_offset_x_1 + circle_size // 2, bar_offset_y_1),
            fill="#727175")

        # Filling Progress Bar

        bar_length = bar_offset_x_1 - bar_offset_x
        # Calculating of length
        # Bar Percentage (final_xp - current_xp)/final_xp

        # Some variables
        progress = (final_xp - xp) * 100 / final_xp
        progress = 100 - progress
        progress_bar_length = round(bar_length * progress / 100)
        pbar_offset_x_1 = bar_offset_x + progress_bar_length

        # Drawing Rectangle
        draw.rectangle((bar_offset_x, bar_offset_y, pbar_offset_x_1, bar_offset_y_1), fill="#11ebf2")
        # Left circle
        draw.ellipse((bar_offset_x - circle_size // 2, bar_offset_y, bar_offset_x + circle_size // 2,
                      bar_offset_y + circle_size), fill="#11ebf2")
        # Right Circle
        draw.ellipse(
            (pbar_offset_x_1 - circle_size // 2, bar_offset_y, pbar_offset_x_1 + circle_size // 2, bar_offset_y_1),
            fill="#11ebf2")

        def convert_int(integer):
            if integer>=1000:
                integer = round(integer / 1000, 2)
                return f'{integer}K'
            else:
                return integer

        # Drawing Xp Text
        text = f"/ {convert_int(final_xp)} XP"
        xp_text_size = draw.textsize(text, font=small_font)
        xp_offset_x = bar_offset_x_1 - xp_text_size[0]
        xp_offset_y = bar_offset_y - xp_text_size[1] - 10
        draw.text((xp_offset_x, xp_offset_y), text, font=small_font, fill="#727175")

        text = f'{convert_int(xp)} '
        xp_text_size = draw.textsize(text, font=small_font)
        xp_offset_x -= xp_text_size[0]
        draw.text((xp_offset_x, xp_offset_y), text, font=small_font, fill="#fff")

        # Placing User Name
        text = member.display_name
        if len(text)>=15:
            text_size=draw.textsize(text, font=small_font)
            text_offset_x = bar_offset_x - 10
            text_offset_y = bar_offset_y - text_size[1] - 10
            draw.text((text_offset_x, text_offset_y), text, font=small_font, fill="#fff")
        else:
            text_size = draw.textsize(text, font=medium_font)
            text_offset_x = bar_offset_x - 10
            text_offset_y = bar_offset_y - text_size[1] - 10
            draw.text((text_offset_x, text_offset_y), text, font=medium_font, fill="#fff")


        # Placing Discriminator
        text = f'#{member.discriminator}'
        text_offset_x += text_size[0] + 10
        text_size = draw.textsize(text, font=small_font)
        text_offset_y = bar_offset_y - text_size[1] - 10
        draw.text((text_offset_x-10, text_offset_y), text, font=small_font, fill="#727175")

        bytes = io.BytesIO()
        img.save(bytes, 'PNG')
        bytes.seek(0)
        return bytes

它会返回一个看起来像这样的图像: 在此处输入图像描述

我希望总 xp 文本字段与 Rank 和 Level 字段内联,但同时在用户名的顶部,你是怎么做到的?

标签: pythondiscord.pypython-imaging-library

解决方案


你还没有定义“ImageDraw”和“ImageFont”


推荐阅读