【问题标题】:Dump characters (glyphs) from TrueType font (TTF) into bitmaps将 TrueType 字体 (TTF) 中的字符(字形)转储到位图中
【发布时间】:2010-04-20 02:19:24
【问题描述】:

我有一个由一堆图标组成的自定义 TrueType 字体 (TTF),我想将其呈现为单独的位图(GIF、PNG 等)以在 Web 上使用。你会认为这是一项简单的任务,但显然不是?这里有大量与 TTF 相关的软件:

http://cg.scs.carleton.ca/~luc/ttsoftware.html

但这都是不同程度的“不是我想要的”、断开的链接和/或很难在现代 Ubuntu 机器上编译 - 例如。 dumpglyphs (C++) 和 ttfgif (C) 都因为模糊的缺失依赖而无法编译。有什么想法吗?

【问题讨论】:

    标签: bitmap truetype


    【解决方案1】:

    试试PILImageDrawImageFont模块

    代码是这样的

    import Image, ImageFont, ImageDraw
    
    im = Image.new("RGB", (800, 600))
    
    draw = ImageDraw.Draw(im)
    
    # use a truetype font
    font = ImageFont.truetype("path/to/font/Arial.ttf", 30)
    
    draw.text((0, 0), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", font=font)
    
    # remove unneccessory whitespaces if needed
    im=im.crop(im.getbbox())
    
    # write into file
    im.save("img.png")
    

    【讨论】:

    • 谢谢!效果很好,我添加了一个稍微调整的版本作为单独的答案。
    【解决方案2】:

    这是 S.Mark 回答的一个有效实现,它将黑色的字符“a”到“z”转储为大小正确的 PNG:

    import Image, ImageFont, ImageDraw
    
    # use a truetype font
    font = ImageFont.truetype("font.ttf", 16)
    im = Image.new("RGBA", (16, 16))
    draw = ImageDraw.Draw(im)
    
    for code in range(ord('a'), ord('z') + 1):
      w, h = draw.textsize(chr(code), font=font)
      im = Image.new("RGBA", (w, h))
      draw = ImageDraw.Draw(im)
      draw.text((-2, 0), chr(code), font=font, fill="#000000")
      im.save(chr(code) + ".png")
    

    【讨论】:

      【解决方案3】:

      其他答案的更简洁、更可靠的版本(为我截断了部分字形):

      import string
      
      from PIL import Image, ImageFont
      
      
      point_size = 16
      font = ImageFont.truetype("font.ttf", point_size)
      
      for char in string.lowercase:
          im = Image.Image()._new(font.getmask(char))
          im.save(char + ".bmp")
      

      我很想知道是否有更好的方法从 font.getmask() 返回的 ImagingCore 对象构造 PIL 图像。

      【讨论】:

      • 请注意,这会创建 .bmp 图像(这是我需要的),但 PIL 的图像转换方法可用于获取其他图像格式。
      【解决方案4】:

      Python3

      上述 S.Mark 答案的工作实现,但在字体文件和字符中添加了更多 cmets、变量和示例。 我试图进行描述,但您可以根据需要简化工作。

      要求:PIL(枕头)

      PILImageDrawImageFont 模块

      # pip install Pillow
      from PIL import Image, ImageFont, ImageDraw
      
      # use a truetype font (.ttf)
      # font file from fonts.google.com (https://fonts.google.com/specimen/Courier+Prime?query=courier)
      font_path = "fonts/Courier Prime/"
      font_name = "CourierPrime-Regular.ttf"
      out_path = font_path
      
      font_size = 16 # px
      font_color = "#000000" # HEX Black
      
      # Create Font using PIL
      font = ImageFont.truetype(font_path+font_name, font_size)
      
      # Copy Desired Characters from Google Fonts Page and Paste into variable
      desired_characters = "ABCČĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽabcčćdđefghijklmnopqrsštuvwxyzž1234567890‘?’“!”(%)[#]{@}/&\<-+÷×=>®©$€£¥¢:;,.*"
      
      # Loop through the characters needed and save to desired location
      for character in desired_characters:
          
          # Get text size of character
          width, height = font.getsize(character)
          
          # Create PNG Image with that size
          img = Image.new("RGBA", (width, height))
          draw = ImageDraw.Draw(img)
          
          # Draw the character
          draw.text((-2, 0), str(character), font=font, fill=font_color)
          
          # Save the character as png
          try:
              img.save(out_path + str(ord(character)) + ".png")
          except:
      
              print(f"[-] Couldn't Save:\t{character}")
      

      【讨论】:

        【解决方案5】:

        使用 Gimp 之类的成像软件来显示您感兴趣的所有字符,然后将每个字符保存到一个文件中。不是快速或高效,但你知道你会得到什么。

        【讨论】:

          猜你喜欢
          • 2012-06-10
          • 2012-03-22
          • 2021-06-08
          • 2013-06-13
          • 2010-10-25
          • 2014-05-24
          • 1970-01-01
          • 1970-01-01
          • 2012-03-18
          相关资源
          最近更新 更多