Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to render text with emojis #1774

Closed
novadev94 opened this issue Mar 16, 2016 · 19 comments
Closed

Unable to render text with emojis #1774

novadev94 opened this issue Mar 16, 2016 · 19 comments
Labels
Enhancement Python 2-only Only on Python 2 (EOL 2020-01-01)
Projects
Milestone

Comments

@novadev94
Copy link

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 #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
Copy link

gthomas commented Jun 16, 2016

👍 I'm also experiencing this issue

@hugovk
Copy link
Member

hugovk commented Jun 16, 2016

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
Copy link

doggan commented Jul 20, 2016

Same issue unfortunately 😦

Mac OS X 10.11.5, Python 2.7.10, Pillow 3.3.0.

@casenjo
Copy link

casenjo commented Dec 4, 2016

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
Copy link
Member

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.

@aclark4life aclark4life added this to the Future milestone Jan 7, 2017
@hugovk hugovk mentioned this issue Sep 20, 2017
@hugovk
Copy link
Member

hugovk commented Sep 20, 2017

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
Copy link

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

@guitar9
Copy link

guitar9 commented Sep 9, 2018

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

amake added a commit to amake/nengobot that referenced this issue Dec 7, 2018
This actually doesn't work because Pillow errors out:
python-pillow/Pillow#1774
@aclark4life aclark4life added this to Backlog in Pillow May 11, 2019
@aclark4life aclark4life moved this from Backlog to Icebox in Pillow May 11, 2019
@quantumpotato
Copy link

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

@nulano
Copy link
Contributor

nulano commented Aug 20, 2019

@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
Copy link
Member

hugovk commented Aug 23, 2019

@quantumpotato What Python version did you use? It works for me with the emoji and code from #1774 (comment) 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
Copy link
Contributor

nulano commented Aug 23, 2019

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 hugovk added the Python 2-only Only on Python 2 (EOL 2020-01-01) label Aug 24, 2019
@hugovk hugovk modified the milestones: Future, 6.2.0 Aug 24, 2019
@hugovk
Copy link
Member

hugovk commented Oct 11, 2019

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

@hugovk hugovk closed this as completed Oct 11, 2019
Pillow automation moved this from Icebox to Closed Oct 11, 2019
@alisentinel
Copy link

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
Copy link
Contributor

nulano commented Jun 18, 2020

@alisentinel The OSError is explained in #1422 (comment):

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 #1422 (comment)):

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
Copy link

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
Copy link

I used pilmoji to get around this!

@jifferon
Copy link

I used pilmoji to get around this!

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

@Abdur-rahmaanJ
Copy link

lol

@python-pillow python-pillow locked as resolved and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Enhancement Python 2-only Only on Python 2 (EOL 2020-01-01)
Projects
Pillow
  
Closed
Development

No branches or pull requests