在 PIL 中使用 Webdings 进行绘图
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前肯定做错了什么。 “symb”作为编码也适用于 wingdings! 对不起,噪音...
I must have done something wrong before. "symb" as the encoding works for wingdings too! Sorry for the noise...
并非所有 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)