How to put skin-toned emojis (black thumbs up, white thumbs up) on images?

I want the output to be like this (found at GitHub):

this (found from github).

But currently I am this:

getting this

The skin tone of an emoji gets separated in my output. How do I fix this? Do I need other libraries?

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

from PIL import Image, ImageDraw, ImageFont

def test(font, out_name):
    fnt = ImageFont.truetype(font, size=109, layout_engine=ImageFont.LAYOUT_RAQM)
    im = Image.new("RGBA", (600, 600), (100, 100, 100, 100))
    draw = ImageDraw.Draw(im)
    draw.text((0, 32), "a\u263A", fill="#faa2", embedded_color=True, font=fnt)
    draw.text((0, 132), "a\u263A", fill="#faa8", embedded_color=True, font=fnt)
    draw.text((0, 232), "a\u263A", fill="#faa", embedded_color=True, font=fnt)
    draw.text((0, 332), "\U0001F3AC\U0001F44B\U0001F3FB\U0001F44B\U0001F3FF", fill="white", embedded_color=True, font=fnt)
    draw.text((0, 432), "a\u263A", fill="#faa2", font=fnt)
    im.show()
    im.save(f"testemoji_{out_name}.png")

test(r"C:\Users\Nulano\Downloads\NotoColorEmoji.ttf", "cbdt")
test("seguiemj.ttf", "colr")

My code:

from PIL import Image, ImageDraw, ImageFont
 
def test(font, out_name):
    fnt = ImageFont.truetype(font, size=109, layout_engine=ImageFont.LAYOUT_BASIC,encoding='utf-16')
    im = Image.new("RGBA", (800, 600), (100, 100, 100, 0))
    draw = ImageDraw.Draw(im)
    draw.text((0, 32), "a\u263A",font=fnt, fill="#faa2", embedded_color=True)
    draw.text((0, 132), "a\u263A",font=fnt, fill="#faa8", embedded_color=True)
    draw.text((0, 232), "a\u263A", fill="#faa",font=fnt, embedded_color=True)
    draw.text((0, 332), "\U0001F3AC\U0001F44B\U0001F3FB\U0001F44B\U0001F3FF",font=fnt, fill="white", embedded_color=True)
    draw.text((0, 432), "a\u263A",font=fnt, fill="#faa2", embedded_color=True)
    im.save(path+"testemoji_{out_name}.png")  
    return im
     
# test(path+"Roboto/NotoColorEmoji.ttf", "cbdt")
test(path+"Roboto/seguiemj.ttf", "colr")

Upvotes: 1

Views: 390

Answers (1)

HansHirse
HansHirse

Reputation: 18925

As you can see, the original code uses

fnt = ImageFont.truetype(font, size=109, layout_engine=ImageFont.LAYOUT_RAQM)

whereas you changed the layout_engine parameter:

fnt = ImageFont.truetype(font, size=109, layout_engine=ImageFont.LAYOUT_BASIC, encoding='utf-16')

So, it seems you need the Raqm library to get advanced text layouting needed for applying those Fitzpatrick Emoji modifiers.

Now, you also seem to use the Windows version of Pillow, for which Raqm is not included by default. Check that by running:

print(PIL.features.check_feature('raqm'))

Initially, I got False as output.

Getting pre-built Windows libraries of libraqm doesn't seem to be that easy, see this Q&A, but following the hints from the comments and answer there, you should be able to get Raqm running under some Windows 10 environment.

After following these hints, the output of the above test changed to True on my machine, and the given, original(!) code does now reproduce the linked images.

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
Pillow:        8.1.0
----------------------------------------

Upvotes: 1

Related Questions