Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Created February 25, 2019 12:58
Show Gist options
  • Save Pullusb/f5d2b66bc3423c4dbbdd7d1daff0c7d9 to your computer and use it in GitHub Desktop.
Save Pullusb/f5d2b66bc3423c4dbbdd7d1daff0c7d9 to your computer and use it in GitHub Desktop.
generate text as images from list txt with python and image magick
import os, random
def quote(text):
return ('"' + text + '"')
texSize = 1024
fontloc = "FONTS/"
out = "out/"
typosize = "80"
doc = "Source_text_lines.txt"
with open(doc) as f:
content = f.readlines()
content = [line[:-1] for line in content] #ugly method to rmove ending "\n"
#[print(c) for c in content]
fontlist = [f for f in os.listdir(fontloc)]
count = 0
for f in fontlist:
fname = f.split(".")[0] #by font:/ for f in fontlist:#get name of the font stripped from extension
#text = quote(fname) #fontName as text
randfont = random.choice(fontlist)
text = quote(random.choice(content)) #random thought
# cmd = "convert -font {font} -size {size}x{size} -pointsize {fontsize} -gravity center label:{text} {outfile}".format(\
# font=quote(fontloc + f), size=texSize, fontsize=typosize, outfile=quote(out+str(count).zfill(2)+fname+".png"),text=text)
###Adaptative to width of sheet
cmd = "convert -font {font} -size {size}x{size} -gravity center label:{text} {outfile}".format(\
font=quote(fontloc + randfont), size=texSize, fontsize=typosize,\
outfile=quote(out+str(count).zfill(2)+fname+".png"),text=text)
print(cmd, "\n")
os.system(cmd)
count += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment