Rendering Emoji with PIL
Asked Answered
N

3

16

I am trying to make images out of tweets, however some of them contain Emojis. I am using PIL to render my images and the Symbola font.

The text is in unicode utf-8 encoding and the Symbola font does include the emojis. Here is an abridged version of the code:

from PIL import Image, ImageFont, ImageDraw
text = u"\U0001f300" #CYCLONE emoji
image = Image.new("RGBA", (100,100), (255,255,255))
font = ImageFont.truetype("Symbola.ttf", 60, encoding='unic')
draw = ImageDraw.Draw(image)
draw.text((0,0), text, (0,0,0), font=font)
image.save("Test.png")
image.show()

This just renders and image with two rectangles instead of the emoji

Would appreciate any help or ideas.

Thanks!

EDIT: As falsetru pointed out, this code does run in Ubuntu, however it doesn't run on Windows or on Mac. Any ideas?

Natelson answered 27/6, 2013 at 21:34 Comment(7)
With your code, I've got cyclone image. I used Symbola.ttf as font_path, 72 as font_size. I ran it with Python 2.7.3 in Ubuntu 12.04.Gerlachovka
You are right, Any ideas why it doesn't work on Windows or Mac?Natelson
The font definitely works under Windows. I can see the cyclone symbol using the font in Word for example.Ingulf
Does PIL work with "unicode codepoints"? Or "UTF-8"? With the latter, I would expect to see hex F09F8C80.Jujitsu
This also works for me on macos.Benyamin
Still does not work on Windows (issue: github.com/python-pillow/Pillow/issues/1774), however a potential workaround is to generate the emoji with ImageMagick instead: see #52129451Ethanethane
somebody has a workaround for rendering whatsapp emojis?Cachucha
V
4

If the symbol CYCLONE u"\U0001f300" (I download a Symbola.tff from web) then is a very simple to use with PIL:

from PIL import Image, ImageDraw, ImageFont, ImageFilter

#configuration
font_size=36
width=500
height=100
back_ground_color=(255,255,255)
font_size=36
font_color=(0,0,0)
unicode_text =u"\U0001f300"
im  =  Image.new ( "RGB", (width,height), back_ground_color )
draw  =  ImageDraw.Draw ( im )
unicode_font = ImageFont.truetype("Symbola.ttf", font_size)
draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
im.show()

Take a look at this

Verdaverdant answered 11/12, 2018 at 22:50 Comment(0)
M
1

There was a bug in Pillow, see #1774 or #3777. This should now be fixed in version 6.1 of Pillow with PR#3780, but only for Python 3.x.

Misvalue answered 20/8, 2019 at 7:53 Comment(0)
B
1

If you are looking to write symbols with your original font, you can do this by merging your font with Symbola.ttf or any emojis font. You can merge the two fonts using fontforge (https://fontforge.org/).

start fontforge, open up your main font,

Click menu: element->merge fonts and choose your emoji font (Symbola.ttf). answer no for any popup dialog.

optionally change your new font's name: element->font info.

finally go to file->generate fonts when done and save it as ttf (TrueType).

Now, you can use your generated font to draw text with emojis!

Badenpowell answered 28/3, 2022 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.