from PIL import Image, ImageDraw, ImageFont import csv import toml conf = None with open('.\settings.conf', mode='r') as c: conf = toml.load(c) inputFile = conf["csv"]["inputFile"] separator = conf["csv"]["separator"] skipFirstLine = conf["csv"]["skipFirstLine"] imgWidth = conf["image"]["width"] imgHeight = conf["image"]["height"] textFont = conf["image"]["font"] bgColor = conf["image"]["bgColor"] fontColor = conf["image"]["fontcolor"] if(type(fontColor) == type([])): fontColor = (fontColor[0],fontColor[1],fontColor[2]) if(type(bgColor) == type([])): bgColor = (bgColor[0],bgColor[1],bgColor[2]) fontSize = conf["image"]["fontsize"] alignment = conf["image"]["alignment"] verticalAlign = conf["image"]["verticalAlign"] outputPath = conf["image"]["outputPath"] with open(inputFile, mode='r') as file: csvFile = csv.reader(file, delimiter=separator) i = 0 for lines in csvFile: if(i == 0 and skipFirstLine): i += 1 continue text = lines[1] outputFileName = lines[0] image = Image.new("RGB", (imgWidth, imgHeight), bgColor) draw = ImageDraw.Draw(image) # select font font = ImageFont.truetype(textFont, size=fontSize) textlength = draw.textlength(text, font=font) # Horizontal alignment of text if(alignment == "center"): xtext = (imgWidth - textlength) // 2 elif(alignment == "left"): xtext = 5 elif(alignment == "right"): xtext = (imgWidth - textlength-3) else: xtext = int(alignment) # Vertical alignment of text if(verticalAlign == "center"): htext = (imgHeight+3 - fontSize) // 2 elif(verticalAlign == "top"): htext = 2 elif(verticalAlign == "bottom"): htext = (imgHeight - fontSize) else: htext = int(verticalAlign) # draw image draw.text((xtext, htext), text, font=font, fill=fontColor) image.save(f"{outputPath}/{outputFileName}.png") i += 1