在 PIL 中使用 Webdings 进行绘图

发布于 2024-07-12 02:56:00 字数 1062 浏览 8 评论 0原文

我有一个使用 PIL 来渲染文本的 Python 程序,它适用于各种字体。 但它只绘制带有 wingdings 或 webdings 的“缺失字形”矩形。

下面是一个尝试绘制每个 unicode 字符的示例:

# Run this with .ttf file path as an argument, and also an encoding if you like.
# It will make 16 PNGs with all the characters drawn.
import sys
import Image, ImageDraw, ImageFont

size = 20
per = 64

chars = 0x10000
perpage = per*per

fontfile = sys.argv[1]
encoding = sys.argv[2] if len(sys.argv) > 2 else ''

font = ImageFont.truetype(sys.argv[1], size, encoding=encoding)

for page in range(0, chars//perpage):

    im = Image.new("RGB", (size*per+30, size*per+30), '#ffffc0')
    draw = ImageDraw.Draw(im)

    for line in range(0, per):
        for col in range(0, per):
            c = page*perpage + line*per + col
            draw.text((col*size, line*size), unichr(c), font=font, fill='black')

    im.save('allchars_%03d.png' % page)

使用 Arial.ttf(或者更好的是 ArialUni.ttf),我得到了 16 个有趣的 PNG。 搜索 PIL 问题时,某些符号字体需要指定其编码。 如果我使用 Symbol.ttf,我会得到所有丢失的字形,直到我指定“symb”作为编码。

如何让 wingdings 工作?

I've got a Python program using PIL to render text, and it works great with all kinds of fonts. But it only draws "missing glyph" rectangles with wingdings or webdings.

Here's a sample that tries to draw every unicode character:

# Run this with .ttf file path as an argument, and also an encoding if you like.
# It will make 16 PNGs with all the characters drawn.
import sys
import Image, ImageDraw, ImageFont

size = 20
per = 64

chars = 0x10000
perpage = per*per

fontfile = sys.argv[1]
encoding = sys.argv[2] if len(sys.argv) > 2 else ''

font = ImageFont.truetype(sys.argv[1], size, encoding=encoding)

for page in range(0, chars//perpage):

    im = Image.new("RGB", (size*per+30, size*per+30), '#ffffc0')
    draw = ImageDraw.Draw(im)

    for line in range(0, per):
        for col in range(0, per):
            c = page*perpage + line*per + col
            draw.text((col*size, line*size), unichr(c), font=font, fill='black')

    im.save('allchars_%03d.png' % page)

With Arial.ttf (or even better, ArialUni.ttf), I get 16 interesting PNGs. Searching for issues with PIL, some symbol fonts need to have their encoding specified. If I use Symbol.ttf, I get all missing glyphs until I specify "symb" as the encoding.

How do I get wingdings to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-07-19 02:56:00

我之前肯定做错了什么。 “symb”作为编码也适用于 wingdings! 对不起,噪音...

I must have done something wrong before. "symb" as the encoding works for wingdings too! Sorry for the noise...

柠檬 2024-07-19 02:56:00

并非所有 wingdings 都映射到 unicode:请参阅 http://www.alanwood.net/demos/ wingdings.html

另外,您只覆盖 基本多语言平面维基百科

Not all of wingdings is mapped to unicode: see http://www.alanwood.net/demos/wingdings.html

Also you're only covering the Basic Multilingual Plane (wikipedia)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文