Rendering Emoji with PIL

2020-03-17 07:40发布

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?

2条回答
可以哭但决不认输i
2楼-- · 2020-03-17 07:50

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.

查看更多
我想做一个坏孩纸
3楼-- · 2020-03-17 07:54

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

查看更多
登录 后发表回答