在 PIL 中使用像素字体

发布于 2024-07-15 10:20:50 字数 849 浏览 7 评论 0 原文

我正在使用 PIL 创建图像,其中包含许多精确放置的文本字符串。 我的第一次尝试是将像素字体转换为 pil 兼容格式,如下所述 这里。 例如,我下载 Silkscreen 字体并将其转换:

otf2bdf -p 8pt -o fonts/slkscr.bdf fonts/slkscr.ttf 
pilfont.py fonts/slkscr.bdf

然后我可以像这样在 PIL 中使用该字体:

import Image, ImageDraw, os, sys, ImageFont
im = Image.new("RGB", (40,10))
draw = ImageDraw.Draw(im)
fn = ImageFont.load('fonts/slkscr.pil')
draw.text((0,0), "Hello", font=fn)
del draw
# write to stdout
im.save(sys.stdout, "PNG")

但是,生成的图像 (“alt)未反映字体应有的外观

我应该使用什么程序来转换和使用像素字体,以便它们按预期呈现?

提前致谢。

I am creating images using PIL that contain numerous exactly placed text strings. My first attempt was to convert pixel fonts into the pil-compatible format as described here. For example, I download the Silkscreen font and convert it:

otf2bdf -p 8pt -o fonts/slkscr.bdf fonts/slkscr.ttf 
pilfont.py fonts/slkscr.bdf

I can then use the font in PIL like so:

import Image, ImageDraw, os, sys, ImageFont
im = Image.new("RGB", (40,10))
draw = ImageDraw.Draw(im)
fn = ImageFont.load('fonts/slkscr.pil')
draw.text((0,0), "Hello", font=fn)
del draw
# write to stdout
im.save(sys.stdout, "PNG")

However, the resulting image (alt text) does not reflect what the font should look like.

What procedure should I be using to convert and use pixel fonts so that they render as intended?

Thanks in advance.

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

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

发布评论

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

评论(1

_蜘蛛 2024-07-22 10:20:50

尤里卡!

只需为 otf2bdf 指定 72 dpi 的分辨率(默认为 100):

otf2bdf -p 8 -r 72 -o fonts/slkscr.bdf fonts/slkscr.ttf

现在, alt text 看起来很棒!

Eureka!

Just needed to specify a resolution of 72 dpi (default is 100) for otf2bdf:

otf2bdf -p 8 -r 72 -o fonts/slkscr.bdf fonts/slkscr.ttf

Now, alt text looks great!

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