镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Use unicode characters #8849

Closed
Closed

Description

@Grise3

Hi, i'd like to use unicode characters like 𝔴 for example.
Here is my script :

from PIL import Image, ImageDraw, ImageFont
width = 1500
height = 400
image = Image.new("RGB", (width, height), "white")
draw = ImageDraw.Draw(image)
text = "✿.ꀘ𝔦𝔴𝔦𝔦.✿ ★ ❀ 𝒜𝓃𝑔𝑒𝓁 ❀ ⚜N€ZUK0⚜ 月が綺麗です ❤️💍🔐 ahio :)"

try:
    font = ImageFont.truetype("arial.ttf", size=40)
except IOError:
    print("Font not found. Using default font.")
    font = ImageFont.load_default()
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
text_width = right - left
text_height = bottom - top
position = ((width - text_width) // 2, (height - text_height) // 2)
draw.text(position, text, fill="black", font=font)
file_name = "image_with_text.png"
image.save(file_name)

print(f"The image has been saved as '{file_name}'.")

here is the result :

Image

for the "classic" emojis, I can use pillmoji, but it doesn't work for the other characters.
I've tried with multiple fonts, and it still don't work

How can I ensure proper rendering of both emojis and decorative Unicode characters with Pillmoji? Is there a workaround or a specific font recommendation?

Activity

  1. radarhere commented on Mar 31, 2025

    @radarhere
    Member

    I don't think there is a specific font recommendation, no. https://fonts.google.com/noto/specimen/Noto+Emoji advertises itself as

    an open source font that has you covered for all your emoji needs, including support for the latest Unicode emoji specification. It has multiple weights and features thousands of emoji.

    but I don't know what your evaluation would be.

    https://www.reddit.com/r/fonts/comments/1ghenf6/does_anyone_know_of_a_monospace_unicode_font_with/ states

    Typically emoji fonts just contain emoji. The ones that do contain both characters pretty much never have them at the same width because it would look like either the latin was too spaced out, or the emoji was too close together. Your best bet is to do it via styling if you can, or to take an emoji font and add the latin characters to it and make the spacing equal in something like Font Forge.

    And indeed, you might be interested in the discussion at #4808 or the #6926 PR, where people are trying to solve the problem of a single font being insufficient by contemplating using multiple fonts at once.

  2. Grise3 commented on Mar 31, 2025

    @Grise3
    Author

    Hi,

    an open source font that has you covered for all your emoji needs, including support for the latest Unicode emoji specification. It has multiple weights and features thousands of emoji.

    yep, that is

    And indeed, you might be interested in the discussion at #4808 or the #6926 PR, where people are trying to solve the problem of a single font being insufficient by contemplating using multiple fonts at once.

    the first one didn't helped me, I'll see #6926 in some hours

  3. radarhere commented on Apr 3, 2025

    @radarhere
    Member

    How can I ensure proper rendering of both emojis and decorative Unicode characters with Pillmoji?

    Wouldn't this be a question for https://github.com/jay3332/pilmoji, rather than for us?

  4. Grise3 commented on Apr 3, 2025

    @Grise3
    Author

    How can I ensure proper rendering of both emojis and decorative Unicode characters with Pillmoji?

    Wouldn't this be a question for https://github.com/jay3332/pilmoji, rather than for us?

    sorry, I wanted to say "How can I ensure proper rendering of both emojis and decorative Unicode characters with Pillow?"

  5. Grise3 commented on Apr 3, 2025

    @Grise3
    Author

    the first one didn't helped me, I'll see #6926 in some hours

    well, can't test it, https://github.com/nulano/Pillow/actions/runs/5568846705 do a 404

  6. radarhere commented on Apr 3, 2025

    @radarhere
    Member

    Ok, I found three fonts that together have all the characters, and put together the following code.

    from PIL import Image, ImageDraw, ImageFont
    width = 1500
    height = 400
    image = Image.new("RGB", (width, height), "white")
    draw = ImageDraw.Draw(image)
    text = "✿.ꀘ𝔦𝔴𝔦𝔦.✿ ★ ❀ 𝒜𝓃𝑔𝑒𝓁 ❀ ⚜N€ZUK0⚜ 月が綺麗です ❤️💍🔐 ahio :)"
    
    fonts = []
    
    def single_character_bytes(font, character):
        im = Image.new("RGB", (40, 40))
        im_draw = ImageDraw.Draw(im)
        im_draw.text((0, 0), character, "white", font)
        return im.tobytes()
    
    for path in (
        "./NotoSansJP-Regular.otf",
        "./NuosuSIL-Regular.ttf",
        "./Symbola.ttf",
    ):
        font = ImageFont.truetype(path, size=40)
        missing_characters = [single_character_bytes(font, character) for character in ("\u25a1", "\ufffd")]
        fonts.append([font, missing_characters])
    
    parts = []
    for character in text:
        for font, missing_characters in fonts:
            if single_character_bytes(font, character) in missing_characters:
                continue
            
            bbox = draw.textbbox((0, 0), character, font=font)
            parts.append([font, character, bbox])
            break
    
    text_width = 0
    text_height = 0
    for part in parts:
        left, top, right, bottom = part[2]
        text_width += right - left
        text_height = max(text_height, bottom - top)
    
    x = (width - text_width) // 2
    for part in parts:
        font, character, bbox = part
        left, top, right, bottom = bbox
        position = (x, (height - text_height) // 2)
        x += right - left
        draw.text(position, character, fill="black", font=font)
    file_name = "image_with_text.png"
    image.save(file_name)
    
    print(f"The image has been saved as '{file_name}'.")

    Image

  7. radarhere commented on Apr 5, 2025

    @radarhere
    Member

    @Grise3 was that helpful?

  8. Grise3 commented on Apr 5, 2025

    @Grise3
    Author

    @Grise3 was that helpful?

    Sorry, I couldn't answer because I was busy. I just tested it and yes, it's helpful, thanks!
    It work fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions