ImageFont 检测丢失的字形(Python Pillow)[重复]

2024-01-29

这是一个简短的example http://pillow.readthedocs.io/en/3.1.x/reference/ImageFont.html.

from PIL import ImageFont, ImageDraw

draw = ImageDraw.Draw(image)

# use a bitmap font
font = ImageFont.load("arial.pil")

draw.text((10, 10), "hello", font=font)

# use a truetype font
font = ImageFont.truetype("arial.ttf", 15)

draw.text((10, 25), "world", font=font)

我想知道字体是否缺少渲染文本中的任何字形。

当我尝试渲染丢失的字形时,我得到一个空方块。

draw.text((10, 10), chr(1234), font=font)
  • 如何以编程方式确定丢失的字形?
  • 如何列出可用的字形ttf file?

这两个问题几乎是一样的。

我更喜欢使用 Pillow 来确定我想要什么。 PyPI 的其他模块也受到欢迎。


有'字体工具 https://github.com/behdad/fonttools' 包,其中包含一个用于读取和查询 TrueType 字体的 python 类。像这样的事情是可能的

from fontTools.ttLib import TTFont
f = TTFont('/path/to/font/arial.ttf')
print(f.getGlyphOrder())      # a list of the glyphs in the order 
                              # they appear
print(f.getReversedGlyphMap() # mapping from glyph names to Id
id = f.getGlyphID('Euro')     # The internal code for the Euro character, 
                              # Raises attribute error if the character
                              # isn't present.

从字符到字形的映射通常很复杂,并且在字体的 cmap 表中定义。这是一个二进制部分,但可以通过以下命令进行检查

f.getTableData('cmap')

一种字体可以有多个 cmap 表。这自由类型接口 https://github.com/rougier/freetype-py还可以读取ttf文件。可以使用 freetype 尝试渲染字符,并查看结果:这会很慢。

import freetype as ft
face = ft.Face('arial.ttf')
face.set_size(25*32)
face.load_char(‽)
bitmap = face.glyph.bitmap.buffer
if bitmap == [255, 255, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 0, 255, 255, 255, 255]:
     print("No interobang in the font")  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ImageFont 检测丢失的字形(Python Pillow)[重复] 的相关文章

随机推荐