Batch font glyphs as images



  • I came across this site, which uses machine learning to generate a typeface.

    In the method, they mention that

    A styleGAN model was trained in RunwayML on a dataset of 2674 Google fonts organised as individual image-per-glyph in Drawbot.

    I was able to find a script that generates images from a single font file, but I need it to be able to handle several thousand. The code is here:

    # use absolute or relative path. This can be OTF or TTF.
    font_path = 'my_font.otf'
    # set the current font to be the one specified above, so we can list its glyph names:
    font(font_path)
    # loop through the list of glyph names:
    for gid, g_name in enumerate(listFontGlyphNames()):
        # the new page is 1000 by 1000 points by default
        newPage()
        fs = FormattedString(
            font=font_path,
            fontSize=500,
            align='center',
            )
        # writing the glyph name into the formatted string
        fs.appendGlyph(g_name)
        # a text box in which the string is used
        textBox(fs, (0, 0, 1000, 800))
        # using the GID for the file name, because macOS is not case sensitive:
        saveImage(f'~/Desktop/{gid}_{g_name}.png')
    

    Could someone please advise me on how I could achieve this? I already have a large folder of ttf files ready to be converted.

    Thanks in advance!!



  • Can you use listdir to get all file names and then open them one by one?

    # This should return a list of all items on the desktop.
    import os
    # Change your_account_name to your account name 
    print(os.listdir('/Users/your_account_name/Desktop/'))
    

Log in to reply