python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
11.95k stars 2.2k forks source link

Unable to render text with emojis #1774

Closed novadev94 closed 4 years ago

novadev94 commented 8 years ago

I want to use Pillow to render text with emojis, to get it easy, I've used a font named Symbola.

# -*- coding: utf-8 -*-
from PIL import Image, ImageFont, ImageDraw
text = u'A😞B'
image = Image.new('RGB', (100, 100), 'white')
font = ImageFont.truetype('Symbola.ttf', size=36, encoding='unic')
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, (0, 0, 0), font=font)
image.show()

But seem like it doesn't succeed image

When I switched to Apple Color Emoji, I have another problem on line

draw.text((0, 0), text, (0, 0, 0), font=font)

# Traceback (most recent call last):
#   File "hello.py", line 13, in <module>
#     draw.text((0, 0), text, (0, 0, 0), font=font)
#   File "/work/emoji/venv/lib/python2.7/site-packages/PIL/ImageDraw.py", line 280, in text
#     mask, offset = font.getmask2(text, self.fontmode)
#   File "/work/emoji/venv/lib/python2.7/site-packages/PIL/ImageFont.py", line 163, in getmask2
#     size, offset = self.font.getsize(text)
# IOError: invalid size handle

So what should I do to be able to render emojis?


May related to https://github.com/python-pillow/Pillow/issues/1422

Also someone has reported a problem about ColorFont support in FreeType (this is a Chinese blog) http://www.cnblogs.com/palance/p/4809872.html

gthomas commented 8 years ago

👍 I'm also experiencing this issue

hugovk commented 8 years ago

symbola.ttf can be found here.

If you replace 😞 with the Python source code U0001F61E I get this on Ubuntu 14.04 (Python 2.7.6, Pillow 3.2.0):

emoji

But I still get this on Windows 7 (Python 2.7.11, Pillow 3.2.0): emoji

(Here's the same thing reported but unsolved on Stack Overflow, confirming the problem on Mac and Windows but not Ubuntu.)

doggan commented 8 years ago

Same issue unfortunately 😦

Mac OS X 10.11.5, Python 2.7.10, Pillow 3.3.0.

casenjo commented 7 years ago

Has anyone had any progress on this issue? I'm trying to do the same thing on OS X Sierra (10.12.1) with Python 3 and Pillow 3.4.2 (and will need to achieve the same on Ubuntu as well)

wiredfool commented 7 years ago

The cnblog linked above google translates as:

I inserted some code inside the debugging information, positioning the problem occurred in the function call FT_Set_Pixel_Sizes inside, which is a freetype function call. Is it not freetype emoji fonts?

Continue to trace freetype, I found freetype 2.5 is called from the beginning to support the color font, I use the 2.6, should be no problem.

I https://gist.github.com/jokertarot/7583938 to find a color font support code, a closer look at the first three steps to use freetype:

1, call Init initialization library

2, construct FreeTypeFace object, generate typeface

3, call SetXXXFont, note that in this step ordinary font and Color font is not the same, ordinary font calls SetNormalFont, internal call FT_Set_Pixel_Sizes; and Color font call SetColorFont, internal call FT_Select_Size

Looking back Pillow-master / _imagingft.c the getfont function, he is not distinguish between NormalFont and ColorFont, according to NormalFont to deal with, so wrong.

This should be the core issue: freetype support color font, but in the specific treatment of the need to use a different interface in the Pillow this layer is not taken into account, it is Pillow can not support color font. To solve the problem, you must make the changes in getfont in Pillow-master / _imagingft.c.

Freetype that support color font code I have not tried, go back and then specialized study.

So it does seem that it's possible with freetype, it's a matter of building it in.

hugovk commented 6 years ago

Current status with Pillow 4.2.1:

# -*- coding: utf-8 -*-
from PIL import Image, ImageFont, ImageDraw
text = u'A😞B'
# text = u'A\U0001F61EB'
image = Image.new('RGB', (100, 50), 'white')
font = ImageFont.truetype('Symbola.ttf', size=36, encoding='unic')
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, (0, 0, 0), font=font)
image.show()
text OS Python Result
u'A😞B' Ubuntu 16.04.2 LTS Python 2.7.12 image
u'A😞B' Ubuntu 16.04.2 LTS Python 3.5.2 image
u'A\U0001F61EB' Ubuntu 16.04.2 LTS Python 2.7.12 image
u'A\U0001F61EB' Ubuntu 16.04.2 LTS Python 3.5.2 image
u'A😞B' macOS Sierra 10.12.5 Python 2.7.13 image
u'A😞B' macOS Sierra 10.12.5 Python 3.6.2 image
u'A\U0001F61EB' macOS Sierra 10.12.5 Python 2.7.13 image
u'A\U0001F61EB' macOS Sierra 10.12.5 Python 3.6.2 image
u'A😞B' Windows 7 Python 2.7.11 image
u'A😞B' Windows 7 Python 3.5.0 image
u'A\U0001F61EB' Windows 7 Python 2.7.11 image
u'A\U0001F61EB' Windows 7 Python 3.5.0 image
odelalleau commented 5 years ago

Just checking if there's any hope to see it fixed on Windows in the not-too-distant future?

guitar9 commented 5 years ago

With Linux Mint it does no work. There are only questionmarks

quantumpotato commented 4 years ago

Bump. I see only a square box on mac. I tried putting u"My ✨ string here", I tried with u"\U0001f44d" too.

nulano commented 4 years ago

@hugovk Can you check this again with Pillow 6.1? This looks to me like another duplicate of #3777 which was fixed in #3780 for Python 3.x.

hugovk commented 4 years ago

@quantumpotato What Python version did you use? It works for me with the emoji and code from https://github.com/python-pillow/Pillow/issues/1774#issuecomment-330820569 on Mac with Python 3, but not Python 2 (so as before). It's quite possible this won't get fixed for Python 2 before the EOL on 2020-01-01.

@nulano I don't have a Windows machine handy at the moment to retest with Python 3. Would be good if someone else can.

nulano commented 4 years ago

I just tried it with Pillow 6.1 on Windows 10 and it works with Python 3.7 but not with Python 2.7, so same as Mac before.

hugovk commented 4 years ago

Closing because the next major release, Pillow 7.0.0, only supports Python 3.

alisentinel commented 4 years ago

Closing because the next major release, Pillow 7.0.0, only supports Python 3.

Same issue Pillow version: 7.1.2 Python version: 3.8.2 font: Apple Color Emoji.ttf OS: ubuntu 20 & windows 10

nulano commented 4 years ago

@alisentinel The OSError is explained in https://github.com/python-pillow/Pillow/issues/1422#issuecomment-636795697:

The FreeType library, which Pillow uses to handle fonts, doesn't allow scaling of bitmap fonts. The only option is to render at a supported size and scale the output yourself.

The sizes available in Apple Color Emoji.ttf are (according to https://github.com/python-pillow/Pillow/issues/1422#issuecomment-140056288):

Testing, I found that the following sizes work - 20, 32, 40, 48, 64, 96, 160

Also note that Pillow currently doesn't support colour in fonts, see #3346. The output will be an outline of the shape.

kubinka0505 commented 3 years ago

If we are talking about a text with colored emoji support WITHOUT using many fonts (>=1), there are APIs that provide emoji images. You could calculate spacing for each line and character and use emojicdn.elk.sh API as an example.

I did some of it in this snippet

Abdur-rahmaanJ commented 7 months ago

I used pilmoji to get around this!

jifferon commented 6 months ago

I used pilmoji to get around this!

Pilmoji is trash which needs access to remove servers. That is a certified clown activity

Abdur-rahmaanJ commented 6 months ago

lol