Simple Plugin to export tga of text characters

Hi there, so I need collection of individual alphanumeric characters in tga format for multiple fonts.

Textures/images should be 1024x1024 resolution. Text centered.

Would like to be able to select font, then the script just goes through all letters a-Z, numbers, and symbols replacing/modifying the text layer and exporting each to tga with alpha.

Paying $25.

Hello @ST33LDI9ITAL and welcome to the forum :slight_smile:

Is that $25 an hour?

1 Like

Hi, thanks. No, that’s total. I really don’t need anything super nice. Just something quick and functional is fine. Would script it myself but not familiar with krita’s api and tight on time atm… I would think an hour would be easy to do if have experience with it…

Solved/Completed. Can be removed.

For anyone else who might be interested in doing something like this, I thought this was an interesting challenge to solve, so I came up with a script. (It took an hour or two, so I didn’t see that this was solved until I finished.)
It’s not perfect: Letters like ‘g’ and ‘y’ that go below the line are cut off, and I’m not sure how to fix it.
Rather than trying to use the Text Tool, which has no API, it’s all done directly in Qt:

# Doesn't actually use anything from Krita, just a convenience to import PyQt
from krita import *

# A script that takes font names, output folders, text, and dimensions,
# and outputs images of each text character.
# Unfortunately characters like "g" and "y" will be cut off...

# Variables -------

# List font names, replace the path with valid ones.
fontAndFolder = {
    "Times New Roman" : "/Users/:/folder/REPLACEME/",
}

# On case insensitive filesystems there can't be A.tga and a.tga,
# these are special cased to append "_upper" and "_lower" to the filename.
upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowerLetters = "abcdefghijklmnopqrstuvwxyz"
# List any filename-usable characters here.
characters = "0123456789"
# If you want a character that cannot be used in a filename
# (or just different naming):
specialChars = {
    ":" : "colon",
}

# Dimensions of the output square here.
outputSize = 1024

# Actual code here -------

paintingRect = QRect(0,0,outputSize,outputSize)

def renderChar(text, font, outputFolder, suffix="", name=""):
    metric = QFontMetrics(font)
    rect = metric.boundingRect(text)

    renderedChar = QImage(paintingRect.size(), QImage.Format_ARGB32)
    p = QPainter()
    p.begin(renderedChar)
    p.setFont(font)
    p.fillRect(paintingRect, Qt.white)
    p.setPen(Qt.black)
    # Unfortunately characters like "g" and "y" will be cut off...
    p.drawText(paintingRect, Qt.AlignCenter, text)
    p.end()

    name = name if name else text
    renderedChar.save(outputFolder+name+suffix+".tga")

def main():
    for fontName, folder in fontAndFolder.items():
        font = QFont(fontName)
        font.setPixelSize(outputSize)

        for l in upperLetters:
            renderChar(l, font, folder, "_upper")
        for l in lowerLetters:
            renderChar(l, font, folder, "_lower")
        for c in characters:
            renderChar(c, font, folder)
        for char, name in specialChars.items():
            renderChar(char, font, folder, "", name)
    
main()
3 Likes

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.