乐知付加密服务平台

如果你有资源, 平台可以帮你实现内容变现, 无需搭建知识付费服务平台。

点击访问官方网站 https://lezhifu.cc

扫码关注公众号 乐知付加密服务平台-微信公众号
字体图片生成 | chenzuoli's blog

字体图片生成

众所周知,我们的文字有各种字体,字体通过字体文件方式供操作系统使用,在需要使用字体图片的场景,我们如何快速生成呢?

这篇文章介绍下,如何通过操作系统自带的字体文件,利用python的pillow包快速生成字体图片。

  1. 各操作系统字体文件路径
    windows\linux\macos:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    dirs = []
    if sys.platform == "win32":
    # check the windows font repository
    # NOTE: must use uppercase WINDIR, to work around bugs in
    # 1.5.2's os.environ.get()
    windir = os.environ.get("WINDIR")
    if windir:
    dirs.append(os.path.join(windir, "fonts"))
    elif sys.platform in ("linux", "linux2"):
    data_home = os.environ.get("XDG_DATA_HOME")
    if not data_home:
    # The freedesktop spec defines the following default directory for
    # when XDG_DATA_HOME is unset or empty. This user-level directory
    # takes precedence over system-level directories.
    data_home = os.path.expanduser("~/.local/share")
    xdg_dirs = [data_home]

    data_dirs = os.environ.get("XDG_DATA_DIRS")
    if not data_dirs:
    # Similarly, defaults are defined for the system-level directories
    data_dirs = "/usr/local/share:/usr/share"
    xdg_dirs += data_dirs.split(":")

    dirs += [os.path.join(xdg_dir, "fonts") for xdg_dir in xdg_dirs]
    elif sys.platform == "darwin":
    dirs += [
    "/Library/Fonts",
    "/System/Library/Fonts",
    os.path.expanduser("~/Library/Fonts"),
    ]
  2. pillow生成图片

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76

    import os
    import random

    import nltk
    from PIL import Image, ImageDraw, ImageFont

    # Download the necessary data from nltk
    nltk.download('inaugural')

    def wrap_text(text, line_length=4):
    """Wraps the provided text every 'line_length' words."""
    words = text.split()
    return "\n".join([" ".join(words[i:i + line_length]) for i in range(0, len(words), line_length)])


    def random_prose_text(line_length=4):
    """Returns a random snippet from the Gutenberg corpus."""
    corpus = nltk.corpus.inaugural.raw()
    start = random.randint(0, len(corpus) - 800)
    end = start + 800
    return wrap_text(corpus[start:end], line_length=line_length)


    def gen_images():
    # get font name and font files
    font_files = []
    for font_dir in dirs:
    for font_file in os.listdir(font_dir):
    if font_file.endswith('.ttf') or font_file.endswith('.ttc'):
    font_path = os.path.join(font_dir, font_file)
    font_name = font_file.split('.')[0]
    font_files.append((font_path, font_name))

    # Generate images for each font file
    for font_path, font_name in font_files:
    # Output the font name so we can see the progress
    print(font_path, font_name)

    # Counter for the image filename
    j = 0
    for i in range(IMAGES_PER_FONT): # Generate 50 images per font - reduced to 10 for now to make things faster
    # Random font size
    font_size = random.choice(range(18, 72))

    if font_path.endswith('.ttc'):
    # ttc fonts have multiple fonts in one file, so we need to specify which one we want
    font = ImageFont.truetype(font_path, font_size, index=0)
    elif font_name in FONT_EXCEPTS:
    continue
    else:
    # ttf fonts have only one font in the file
    font = ImageFont.truetype(font_path, font_size)

    # Determine the number of words that will fit on a line
    font_avg_char_width = font.getbbox('x')[2]
    words_per_line = int(800 / (font_avg_char_width * 5))
    prose_sample = random_prose_text(line_length=words_per_line)

    # print("generate font image: " + str(prose_sample))
    for text in [prose_sample]:
    img = Image.new('RGB', (800, 400), color="white") # Canvas size
    draw = ImageDraw.Draw(img)

    # Random offsets, but ensuring that text isn't too far off the canvas
    offset_x = random.randint(-20, 10)
    offset_y = random.randint(-20, 10)

    # vary the line height
    line_height = random.uniform(0, 1.25) * font_size
    draw.text((offset_x, offset_y), text, fill="black", font=font, spacing=line_height)

    j += 1
    output_file = os.path.join(GEN_IMAGES_DIR, f"{font_name}_{j}.png")
    img.save(output_file)

源代码都记录在这里了:
https://github.com/chenzuoli/font-identifier

本代码参考开源项目:https://huggingface.co/gaborcselle/font-identifier

好了,记录到这里,持续更新中。

记录问题也是一种修行。

修行


书山有路勤为径,学海无涯苦作舟。

欢迎关注微信公众号:【乐知付加密平台】,您的网络资源可变现
乐知付加密平台

一起学习,一起进步。

-------------本文结束感谢您的阅读-------------