【问题标题】:How to render emojis as images in Python under Windows?如何在 Windows 下的 Python 中将 emojis 渲染为图像?
【发布时间】:2019-02-07 06:47:38
【问题描述】:

我的目标是生成(在 Windows 下的 Python 中)渲染任何 unicode 字符的位图图像,尤其是表情符号。我已经安装了几种对表情符号友好的字体(包括Symbola)用于测试目的。

到目前为止,我已经尝试过 PIL、matplotlib 和 pygame,但这些都不能在 Windows 下完成(前两个显然可以在某些版本的 Linux / MacOS 上完成,而 pygame 明确限于字符以上到 0xffff,排除表情符号)。

我发现reportlab 能够生成带有表情符号的 PDF(而它的位图渲染器无法正确渲染它们),但我仍然需要找到一种方法从 PDF 中提取表情符号并将其转换为位图.我觉得必须有一个更简单的方法......

注意:这个问题与 Rendering Emoji with PIL 有关,但如果另一个库可以完成这项工作,我不一定想使用 PIL

【问题讨论】:

    标签: python windows unicode emoji


    【解决方案1】:

    我最终在Is there any good python library for generating and rendering text in image format? 中找到了解决方案。虽然它基于第三方可执行文件,但如前所述,它很容易用 Python 封装。

    具体步骤如下:

    1. https://www.imagemagick.org/script/download.php#windows 安装 ImageMagick
    2. 将环境变量MAGICK_HOME设置为安装文件夹
    3. 安装 Pillow 以便能够在 Python 中轻松操作生成的图像 (conda install pillow)
    4. https://fontlibrary.org/en/font/symbola 下载并安装 Symbola 字体

    还有我的测试脚本:

    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()
    

    【讨论】:

    • @KTWorks 这很有可能,这个问题是专门针对 Windows 的。对于 Linux,PIL 至少适用于某些发行版(请参阅 2017 年 9 月 20 日 github.com/python-pillow/Pillow/issues/1774 中的评论)
    • 我正在寻找类似 whatsapp 中的彩色表情符号
    猜你喜欢
    • 2021-12-08
    • 2019-02-08
    • 2017-11-16
    • 2011-03-10
    • 2020-10-05
    • 2012-05-30
    相关资源
    最近更新 更多