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

Is it possible to use colored Whatsapp/Facebook emoji? #3346

Closed

Description

@guitar9

What did you do?

from PIL import Image, ImageDraw, ImageFont
import textwrap

astr = ''' 👩‍⚕️: H👩‍'''
para = textwrap.wrap(astr, width=40)

MAX_W, MAX_H = 400, 100 + 30 * len(para)
im = Image.new('RGB', (MAX_W, MAX_H), (0, 45, 0, 0))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('DejaVuSans.ttf', size=18)

current_h, pad = 50, 10
for line in para:
    w, h = draw.textsize(line, font=font)
    draw.text(((MAX_W - w) / 2, current_h), line, font=font)
    current_h += h + pad
im.save('test.png')

What did you expect to happen?

Is it possible to show colored Whatsapp oder Facebook Emoji?

What actually happened?

For the woman emoji i only get a white rect.

What versions of Pillow and Python are you using?

Pillow 3.1.2
Python 3.5.2

Activity

  1. guitar9 commented on Sep 11, 2018

    @guitar9
    Author

    I think this is not implemented. But have somebody ideas to do a workaround? For example to tranform the emoji to a image and open the image and render it in text.
    http://www.unicode.org/emoji/charts/full-emoji-list.html
    https://github.com/carpedm20/emoji

  2. hugovk commented on Sep 11, 2018

    @hugovk
    Member

    A workaround is to call something like ImageMagick to create an image of the emoji, then open that.

    For example, https://stackoverflow.com/a/52131155/724176

  3. guitar9 commented on Sep 11, 2018

    @guitar9
    Author

    this does not work under linux

  4. hugovk commented on Nov 20, 2019

    @hugovk
    Member

    Does this still happen?

    How about when using a Noto font?

  5. Exploder98 commented on Apr 9, 2020

    @Exploder98

    Noto Emoji gives black and white emojis, and Noto Color Emoji fails to load with OSError: invalid pixel size. I'm using Pillow 7.0.0 on Ubuntu.

  6. nulano commented on Apr 10, 2020

    @nulano
    Contributor

    Noto Emoji gives black and white emojis, and Noto Color Emoji fails to load with OSError: invalid pixel size. I'm using Pillow 7.0.0 on Ubuntu.

    This error is due to a freetype limitation. Bitmap fonts don't support scaling with freetype, so you must specify a valid size, which is 109 for Noto Color Emoji.

    Additionally, freetype must be built with libpng to support this font (png bitmaps). However, due to #960, bitmap fonts will not work with Pillow at all. And even if they did, you would get a grayscale version of the emojis, because Pillow only returns one channel from the C layer font functions.

    Note: I am working on improving some of this, I just need to write some tests to submit a PR.

  7. boringcactus commented on Jun 19, 2020

    @boringcactus

    it looks like FreeType 2.10.0 adds support for COLR/CPAL tables, which Noto Color Emoji uses for scalable color emoji glyphs. could this be taken advantage of to add support for this type of emoji font to Pillow?

  8. nulano commented on Jun 19, 2020

    @nulano
    Contributor

    @boringcactus I've got it partially implemented, but it is not PR ready. I'm not sure when I'll have time to work on it, but it will be either next week or a few weeks later.

  9. GanerCodes commented on Aug 30, 2020

    @GanerCodes

    @boringcactus I've got it partially implemented, but it is not PR ready. I'm not sure when I'll have time to work on it, but it will be either next week or a few weeks later.

    cough

  10. nulano commented on Aug 30, 2020

    @nulano
    Contributor

    I ended up working on #4724 first 😄 and postponed this until that gets merged to avoid merge conflicts. Thinking about it again, I don't think these two actually conflict too badly, so perhaps I can submit this soon as well. I'm hoping both of these can be included in version 8.0.0 planned for release in 6 weeks.

  11. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    Ah thanks, if you have the patch could you send it to me? I can implement it locally

  12. nulano commented on Aug 31, 2020

    @nulano
    Contributor

    See my branch nulano/Pillow@06ba083...nulano:ft-color. IIRC it should work, but there are some extra unnecessary changes that should be removed, and parts of it can be simplified, before submitting it as a PR. Please treat it as a WIP, the API will likely change in the final version.

  13. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    Hey, can you send me a compiled version for windows?

  14. 1 remaining item

  15. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    I'm dumb, how do I install it with that?

  16. hugovk commented on Aug 31, 2020

    @hugovk
    Member

    Not dumb, it's not so obvious! :)

    If you click dist"-ft-color" under Artifacts it'll download a zip file.

    Extract that, and then pip install filename.whl, where filename.whl is the one that closest matches your system (e.g. cp38 for Python 3.8).

  17. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    Hey, thank you so much, the new version installed correctly
    However, it seems that fonts with color don't work, can you tell me what is wrong with this code?

    # -*- coding: utf-8 -*-
    from PIL import Image
    from PIL import ImageFont
    from PIL import ImageDraw
    
    import os
    
    im = Image.open('purple.png')
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype('seguiemj.ttf', 64, encoding = 'unic')
    textToDraw = u'😀'
    print(textToDraw)
    draw.text((20,20), textToDraw, (255,255,255), font=font)
    im.show()
  18. nulano commented on Aug 31, 2020

    @nulano
    Contributor

    For backwards compatibility you have enable color support using draw.text(..., embedded_color=True). This parameter might be renamed in the final version.

  19. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    POG IT WORKS thank you so much man

  20. GanerCodes commented on Aug 31, 2020

    @GanerCodes

    Also, is there a way to have a secondary font for mission glyphs? like many fonts don't have emoji and ideally I want to support them still

  21. nulano commented on Aug 31, 2020

    @nulano
    Contributor

    Also, is there a way to have a secondary font for mission glyphs? like many fonts don't have emoji and ideally I want to support them still

    See #4808. Edit: fix link typo.

    The best you can do for now is use unicodedata.category to break up text into "spans" each rendered separately with their own font. You can use font.getsize(...)[0] or draw.textsize(...)[0] as the horizontal offset between spans for now (#4724 proposes adding a faster and more precise font.getlength(...)). You will also have to handle linebreaks yourself, see draw.multiline_text for inspiration.

  22. nulano commented on Oct 14, 2020

    @nulano
    Contributor

    Pillow 8.0.0 has now been released, adding support for CBDT and COLR fonts.
    FreeType (and hence also Pillow ) does not yet support SBIX or SVG fonts.
    To enable color support, use draw.text(..., embedded_color=True).

    Edit: SBIX is supported in Pillow 8.0.0.
    Edit2: FreeType 2.12 added support for SVG fonts via callbacks, see #6170 for implementing this in Pillow.

  23. jdhao commented on Mar 31, 2022

    @jdhao

    @nulano I tried Pillow 9.0 and it seems that I still can not use noto color emoji font. The same error persists:

    OSError: invalid pixel size

  24. radarhere commented on Mar 31, 2022

    @radarhere
    Member

    @jdhao would you be able to create a new issue with a self-contained example, and information about your environment?

  25. jdhao commented on Mar 31, 2022

    @jdhao

    Okay, will do it: #6166.

  26. shula commented on Feb 26, 2025

    @shula

    @nulano How to obtain the size of a font?
    How do you know that NotoColorEmoji will render in 109 pixels?

  27. radarhere commented on Feb 28, 2025

    @radarhere
    Member

    I'm not sure what kind of answer you're interested in.

    If you're asking how you can determine that using Pillow,

    from PIL import ImageFont
    
    for i in range(1, 500):
      try:
        font = ImageFont.truetype("NotoColorEmoji.ttf", i)
        print("Font size "+str(i)+" is available")
      except OSError:
        pass

    will try all font sizes between 1 and 500 and then only print

    Font size 109 is available
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions