14

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?

CnrL
  • 2,558
  • 21
  • 28
colorstain
  • 141
  • 2
  • 4
  • 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. – falsetru Jun 28 '13 at 02:30
  • 1
    You are right, Any ideas why it doesn't work on Windows or Mac? – colorstain Jul 02 '13 at 01:35
  • The font definitely works under Windows. I can see the cyclone symbol using the font in Word for example. – CnrL Jul 05 '13 at 09:53
  • Does PIL work with "unicode codepoints"? Or "UTF-8"? With the latter, I would expect to see hex `F09F8C80`. – Rick James Jan 17 '18 at 21:39
  • This also works for me on macos. – jjj Feb 03 '18 at 15:42
  • 2
    Still does not work on Windows (issue: https://github.com/python-pillow/Pillow/issues/1774), however a potential workaround is to generate the emoji with ImageMagick instead: see https://stackoverflow.com/questions/52128951/how-to-render-emojis-as-images-in-python-under-windows – tiho Sep 01 '18 at 18:47
  • somebody has a workaround for rendering whatsapp emojis? – otto Sep 11 '18 at 20:54

3 Answers3

2

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

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.

Nulano
  • 1,148
  • 13
  • 27
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!

AKMalkadi
  • 782
  • 1
  • 5
  • 18