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()