【问题标题】:ImageFont detect missing glyph (Python Pillow) [duplicate]ImageFont 检测丢失的字形(Python Pillow)[重复]
【发布时间】:2017-01-11 23:44:32
【问题描述】:

这是一个简短的example

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 文件中列出可用字形?

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

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

【问题讨论】:

  • 相关stackoverflow.com/questions/35436932/…(但解决办法是编辑字体)
  • 谢谢,最好能列出可用的字形。
  • 那么是否有任何解决方案可以知道缺少特定字形?
  • 不幸的是我没有找到解决方案。我使用启发式方法来检测给定范围内的无效字形。

标签: python python-3.x pillow


【解决方案1】:

有一个“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 表。 freetype interface 也可以读取 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")  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多