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

Is it possible to use colored Whatsapp/Facebook emoji? #3346

Closed
guitar9 opened this issue Sep 10, 2018 · 25 comments
Closed

Is it possible to use colored Whatsapp/Facebook emoji? #3346

guitar9 opened this issue Sep 10, 2018 · 25 comments

Comments

@guitar9
Copy link

guitar9 commented Sep 10, 2018

What did you do?

from PIL import Image, ImageDraw, ImageFont
import textwrap

astr = ''' πŸ‘©β€βš•οΈ: HπŸ‘©β€'''
para = textwrap.wrap(astr, width=40)

MAX_W, MAX_H = 400, 100 + 30 * len(para)
im = Image.new('RGB', (MAX_W, MAX_H), (0, 45, 0, 0))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('DejaVuSans.ttf', size=18)

current_h, pad = 50, 10
for line in para:
    w, h = draw.textsize(line, font=font)
    draw.text(((MAX_W - w) / 2, current_h), line, font=font)
    current_h += h + pad
im.save('test.png')

What did you expect to happen?

Is it possible to show colored Whatsapp oder Facebook Emoji?

What actually happened?

For the woman emoji i only get a white rect.

What versions of Pillow and Python are you using?

Pillow 3.1.2
Python 3.5.2

@guitar9
Copy link
Author

guitar9 commented Sep 11, 2018

I think this is not implemented. But have somebody ideas to do a workaround? For example to tranform the emoji to a image and open the image and render it in text.
http://www.unicode.org/emoji/charts/full-emoji-list.html
https://github.com/carpedm20/emoji

@hugovk
Copy link
Member

hugovk commented Sep 11, 2018

A workaround is to call something like ImageMagick to create an image of the emoji, then open that.

For example, https://stackoverflow.com/a/52131155/724176

@guitar9
Copy link
Author

guitar9 commented Sep 11, 2018

this does not work under linux

@hugovk
Copy link
Member

hugovk commented Nov 20, 2019

Does this still happen?

How about when using a Noto font?

@Exploder98
Copy link

Noto Emoji gives black and white emojis, and Noto Color Emoji fails to load with OSError: invalid pixel size. I'm using Pillow 7.0.0 on Ubuntu.

@nulano
Copy link
Contributor

nulano commented Apr 10, 2020

Noto Emoji gives black and white emojis, and Noto Color Emoji fails to load with OSError: invalid pixel size. I'm using Pillow 7.0.0 on Ubuntu.

This error is due to a freetype limitation. Bitmap fonts don't support scaling with freetype, so you must specify a valid size, which is 109 for Noto Color Emoji.

Additionally, freetype must be built with libpng to support this font (png bitmaps). However, due to #960, bitmap fonts will not work with Pillow at all. And even if they did, you would get a grayscale version of the emojis, because Pillow only returns one channel from the C layer font functions.

Note: I am working on improving some of this, I just need to write some tests to submit a PR.

@boringcactus
Copy link

it looks like FreeType 2.10.0 adds support for COLR/CPAL tables, which Noto Color Emoji uses for scalable color emoji glyphs. could this be taken advantage of to add support for this type of emoji font to Pillow?

@nulano
Copy link
Contributor

nulano commented Jun 19, 2020

@boringcactus I've got it partially implemented, but it is not PR ready. I'm not sure when I'll have time to work on it, but it will be either next week or a few weeks later.

@GanerCodes
Copy link

@boringcactus I've got it partially implemented, but it is not PR ready. I'm not sure when I'll have time to work on it, but it will be either next week or a few weeks later.

cough

@nulano
Copy link
Contributor

nulano commented Aug 30, 2020

I ended up working on #4724 first πŸ˜„ and postponed this until that gets merged to avoid merge conflicts. Thinking about it again, I don't think these two actually conflict too badly, so perhaps I can submit this soon as well. I'm hoping both of these can be included in version 8.0.0 planned for release in 6 weeks.

@GanerCodes
Copy link

Ah thanks, if you have the patch could you send it to me? I can implement it locally

@nulano
Copy link
Contributor

nulano commented Aug 31, 2020

See my branch nulano/Pillow@06ba083...nulano:ft-color. IIRC it should work, but there are some extra unnecessary changes that should be removed, and parts of it can be simplified, before submitting it as a PR. Please treat it as a WIP, the API will likely change in the final version.

@GanerCodes
Copy link

Hey, can you send me a compiled version for windows?

@nulano
Copy link
Contributor

nulano commented Aug 31, 2020

I've re-triggered the GHA run, you can get wheels there: https://github.com/nulano/Pillow/actions/runs/232647949

@GanerCodes
Copy link

I'm dumb, how do I install it with that?

@hugovk
Copy link
Member

hugovk commented Aug 31, 2020

Not dumb, it's not so obvious! :)

If you click dist"-ft-color" under Artifacts it'll download a zip file.

Extract that, and then pip install filename.whl, where filename.whl is the one that closest matches your system (e.g. cp38 for Python 3.8).

@GanerCodes
Copy link

GanerCodes commented Aug 31, 2020

Hey, thank you so much, the new version installed correctly
However, it seems that fonts with color don't work, can you tell me what is wrong with this code?

# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

import os

im = Image.open('purple.png')
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('seguiemj.ttf', 64, encoding = 'unic')
textToDraw = u'πŸ˜€'
print(textToDraw)
draw.text((20,20), textToDraw, (255,255,255), font=font)
im.show()

@nulano
Copy link
Contributor

nulano commented Aug 31, 2020

For backwards compatibility you have enable color support using draw.text(..., embedded_color=True). This parameter might be renamed in the final version.

@GanerCodes
Copy link

POG IT WORKS thank you so much man

@GanerCodes
Copy link

Also, is there a way to have a secondary font for mission glyphs? like many fonts don't have emoji and ideally I want to support them still

@nulano
Copy link
Contributor

nulano commented Aug 31, 2020

Also, is there a way to have a secondary font for mission glyphs? like many fonts don't have emoji and ideally I want to support them still

See #4808. Edit: fix link typo.

The best you can do for now is use unicodedata.category to break up text into "spans" each rendered separately with their own font. You can use font.getsize(...)[0] or draw.textsize(...)[0] as the horizontal offset between spans for now (#4724 proposes adding a faster and more precise font.getlength(...)). You will also have to handle linebreaks yourself, see draw.multiline_text for inspiration.

@nulano
Copy link
Contributor

nulano commented Oct 14, 2020

Pillow 8.0.0 has now been released, adding support for CBDT and COLR fonts.
FreeType (and hence also Pillow ) does not yet support SBIX or SVG fonts.
To enable color support, use draw.text(..., embedded_color=True).

Edit: SBIX is supported in Pillow 8.0.0.
Edit2: FreeType 2.12 added support for SVG fonts via callbacks, see #6170 for implementing this in Pillow.

@hugovk hugovk closed this as completed Dec 2, 2020
@jdhao
Copy link

jdhao commented Mar 31, 2022

@nulano I tried Pillow 9.0 and it seems that I still can not use noto color emoji font. The same error persists:

OSError: invalid pixel size

@radarhere
Copy link
Member

@jdhao would you be able to create a new issue with a self-contained example, and information about your environment?

@jdhao
Copy link

jdhao commented Mar 31, 2022

Okay, will do it: #6166.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants