Emoji rendering with PIL

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

The text is unicode utf-8 encoded, and the Symbola font includes emojis. Here is a shortened 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() 

It just displays and the image with two rectangles instead of emoji

Thank you for any help or ideas.

Thanks!

EDIT: As falsetru pointed out, this code works on Ubuntu, however it does not run on Windows or Mac. Any ideas?

+8
source share
2 answers

If the symbol is CYCLONE u "\ U0001f300" (I download Symbola.tff from the Internet), it is very simple to use it 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

0
source

An error has occurred in the 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.

0
source

Source: https://habr.com/ru/post/948278/


All Articles