5

I am trying to insert emoji into an image using pillow library but the result is not what I want.

I want the emoji look exactly the same as it looks in iphone, but the result is a black and white ugly emoji.

this is my code:

from PIL import Image, ImageDraw, ImageFont

back_ground_color = (255, 255, 255)
font_color = (0, 0, 0)

unicode_text = u"\U0001f618"
im = Image.new("RGB", (200, 200), back_ground_color)
draw = ImageDraw.Draw(im)
unicode_font = ImageFont.truetype("Symbola.ttf", 36)
draw.text((80, 80), unicode_text, font=unicode_font, fill=font_color)
im.show()

and below is the reult:

enter image description here

So what am i doing wrong?

beh-f
  • 103
  • 1
  • 9
  • What you get is the pic from the [symbola.ttf](https://github.com/stv0g/unicode-emoji/blob/master/symbola/1f618.png). You may need a different source in my opinion. – NWiogrhkt Oct 07 '20 at 23:21
  • 1
    @NWiogrhkt I just changed my font to [Apple Color Emoji.ttf](https://github.com/potyt/fonts/blob/master/macfonts/Apple%20Color%20Emoji/Apple%20Color%20Emoji.ttf) and now i am getting a weird error. `OSError: invalid size handle` – beh-f Oct 07 '20 at 23:31
  • [here](https://github.com/python-pillow/Pillow/issues/3066) is a issue about this problem with a couple of hints. – NWiogrhkt Oct 08 '20 at 00:19

4 Answers4

4

Pillow 8.0.0 has now been released by adding support for COLR fonts.

Source: https://github.com/python-pillow/Pillow/pull/4955

Mehdi Rahimi
  • 167
  • 1
  • 9
3

It seems that this is not yet implemented in pillow as can be seen here: https://github.com/python-pillow/Pillow/issues/3346 and here : https://github.com/python-pillow/Pillow/pull/4955

Maybe you need to try a workaround like described here: How to render emojis as images in Python under Windows?

daniel_heg
  • 31
  • 1
  • I just used the last link you provided and everything worked except that the font he has used is Symbola.ttf which outputs a b&w emoji. I then tried to install Apple Color Emoji but windows couldn't install it. – beh-f Oct 08 '20 at 00:31
1

Add embedded_color=True to your ImageDraw.Draw(image).text so like this:

ImageDraw.Draw(image).text((0, 332), "\U0001F3AC\U0001F44B\U0001F3FB\U0001F44B\U0001F3FF", fill="white", embedded_color=True, font=fnt)
MaxCodes
  • 82
  • 10
1

It seems that Symbola is not a color emoji font, you can not show color emoji with it!

You need to try those color emoji fonts, like NotoColorEmoji, AppleColorEmoji or segoeuiemoji. The first two are bit-map color emoji font and the last one is vector color emoji font.

I have tested the three font and they work well for me:

from PIL import Image, ImageDraw, ImageFont

back_ground_color = (255, 255, 255)

unicode_text = "\U0001f602"
im = Image.new("RGB", (500, 500), back_ground_color)
draw = ImageDraw.Draw(im)

# seem only black and white, no color info
# unicode_font = ImageFont.truetype(r"E:\symbola-font\Symbola-AjYx.ttf", 30)

# bit-map based, src: https://github.com/googlefonts/noto-emoji/blob/main/fonts/NotoColorEmoji.ttf
# unicode_font = ImageFont.truetype(r"E:\NotoColorEmoji.ttf", 109)

# bit-map based, src: https://github.com/samuelngs/apple-emoji-linux/releases
unicode_font = ImageFont.truetype(r"E:\AppleColorEmoji.ttf", 137)

# font from here: https://fontsdata.com/132714/segoeuiemoji.htm (for test only)
# svg-based, works with different font size
# unicode_font = ImageFont.truetype(r"E:\segoeuiemoji\seguiemj.ttf", 50)

draw.text((80, 80), unicode_text, font=unicode_font, embedded_color=True)
im.show()

Pillow version: 8.3.2

Ref

https://github.com/python-pillow/Pillow/pull/4955

jdhao
  • 24,001
  • 18
  • 134
  • 273