tiho
tiho

Reputation: 6935

How to render emojis as images in Python under Windows?

My goal is to generate (in Python under Windows) a bitmap image rendering any unicode character, including in particular emojis. I have installed several emoji-friendly fonts (including Symbola) for testing purpose.

So far I've tried PIL, matplotlib and pygame, but none of these are able to do it under Windows (the first two apparently can do it on some versions of Linux / MacOS, while pygame is explicitly limited to characters up to 0xffff, which rules out emojis).

I found that reportlab is able to generate a PDF with emojis (while its bitmap renderer fails to properly render them), but I still need to find a way to extract the emoji character from the PDF and convert it to bitmap. I feel like there has to be a simpler way...

NB: this question is related to Rendering Emoji with PIL but I do not necessarily want to use PIL if another library can do the job

Upvotes: 4

Views: 4190

Answers (1)

tiho
tiho

Reputation: 6935

I eventually found a solution in Is there any good python library for generating and rendering text in image format?. Although it is based on a third-party executable, as mentioned it is easy to wrap in Python.

Exact steps were as follows:

  1. Install ImageMagick from https://www.imagemagick.org/script/download.php#windows
  2. Set environment variable MAGICK_HOME to installation folder
  3. Install Pillow to be able to manipulate easily the resulting image in Python (conda install pillow)
  4. Download and install the Symbola font from https://fontlibrary.org/en/font/symbola

And my test script:

import os
import subprocess

import PIL.Image

to_render = '🤓'
output_file = 'rendered_emoji.bmp'

subprocess.run([
    os.path.join(os.environ['MAGICK_HOME'],  'magick.exe'),
    'convert', '-font', 'Symbola', '-size', '50x50',
    '-gravity', 'center', f'label:{to_render}', output_file])
image = PIL.Image.open(output_file)
image.show()

Upvotes: 5

Related Questions