Younes Lmariouh
Younes Lmariouh

Reputation: 11

Draw a Special text on a Picture with PIL

I just wanted to write some text on picture using PIL and Python 3.8.6 in my Discord Bot while a member joined but it did not shows correctly, Here is my code and result picture.

    background = Image.open("./photos/Sans titre 1.png").convert("RGBA")
    basewidth = 227
    img = Image.open(BytesIO(response.content)).convert("RGBA")
    wpercent = (basewidth/float(img.size[0]))
    hsize = int((float(img.size[1])*float(wpercent)))
    img = img.resize((basewidth,hsize), Image.ANTIALIAS)
    cercle = Image.open("./photos/cercle_orange.png").convert("RGBA")
    img_w, img_h = img.size
    cr_w,cr_h = cercle.size
    # make a blank image for the text, initialized to transparent text color
    txt = Image.new("RGBA", background.size, (255,255,255,0))
    msg = "WELCOME"
    
    new_member = u"{}#{}".format(member.name,member.discriminator)
    fnt = ImageFont.truetype("./fonts/DejaVuSans.ttf", 80)
    fntname = ImageFont.truetype("./fonts/DejaVuSans.ttf",50)
    # get a drawing context
    d = ImageDraw.Draw(txt)
    bg_w, bg_h = background.size
    offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
    w, h = d.textsize(msg,font=fnt)
    w2, h2 = d.textsize(new_member,font=fntname)
    d.text(((bg_w-w)/2,((bg_h-h)/2)+160), msg, font=fnt, fill='#FFAA39')
    d.text((((bg_w-w2)/2),((bg_h-h2)/2)+220), new_member, font=fntname, fill=(255,255,255,255))
    background.paste(profile, offset, profile)

my bot welcome picture in Discord

Discord Welcome Picture

i already search how to transfer the text to unicode and it didn't work however i changed the font that i use in my bot code, i want it to show the name 𝕯𝖆𝖗𝐀 𝔇𝔒𝕧𝒾𝖑 correctly in my code as this second picture from Koya Bot: Koya Welcome Picture in Discord Koya Welcome Picture

i Hope you can help me,Thank you guys for listening.

Upvotes: 1

Views: 492

Answers (1)

Shunya
Shunya

Reputation: 2449

This behaviour of displaying squares typically happens when the font cannot render the specified characters, which could mean that the font is corrupted or simply that the specified font is missing glyphs for those specific characters. Most commonly used fonts have a wide range of glyphs that cover common scripts (latin, greek, hebrew, cyrillic, arabic, etc).

You might want to try changing the font to see if it that works, if it does, then the problem is the font, you might want to try finding another source for that font to see if it includes the necessary glyphs, or directly change the font.

Upvotes: 1

Related Questions