How do I center a text with the python PIL library?

Asked

Viewed 37 times

1

so guys, I did an event on_member_join with an image using the Pillow library, and I’m looking for help to make the PIL calculate the text size and center it according to the user’s name, can help me?

    ```url = requests.get(member.avatar_url_as(format="png"))
    avatar = Image.open(BytesIO(url.content))
    avatar = avatar.resize((210, 210));
    bigsize = (avatar.size[0] * 2,  avatar.size[1] * 2)
    mask = Image.new('L', bigsize, 0)
    draw = ImageDraw.Draw(mask)
    draw.ellipse((0, 0) + bigsize, fill=255)
    mask = mask.resize(avatar.size, Image.ANTIALIAS)
    avatar.putalpha(mask)

    saida = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
    saida.putalpha(mask)

    fundo = Image.open('cogs/img/bem-vindo.png')
    fonte = ImageFont.truetype('cogs/img/college.ttf',42)
    
    escrever = ImageDraw.Draw(fundo)
    escrever.text(xy=(230,345), text=str(member),fill=(0,0,0),font=fonte)
    fundo.paste(saida, (357, 39), saida)

    fundo.save("cogs/img/welcome.png")```

only that it only stays in a certain location, and wanted to center it in space (https://cdn.discordapp.com/attachments/738572311107469354/767727707710160896/unknown.png)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.