colorstain
colorstain

Reputation: 181

Rendering Emoji with PIL

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?

Upvotes: 18

Views: 12297

Answers (3)

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()

Upvotes: 5

AKMalkadi
AKMalkadi

Reputation: 982

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!

Upvotes: 3

Nulano
Nulano

Reputation: 1343

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.

Upvotes: 1

Related Questions