beh-f
beh-f

Reputation: 118

Insert colorful emoji into an image(Python)

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?

Upvotes: 5

Views: 4998

Answers (4)

jdhao
jdhao

Reputation: 28487

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

Upvotes: 2

MaxCodes
MaxCodes

Reputation: 104

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)

Upvotes: 1

Mehdi Rahimi
Mehdi Rahimi

Reputation: 177

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

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

Upvotes: 4

daniel_heg
daniel_heg

Reputation: 31

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?

Upvotes: 3

Related Questions