-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
66 lines (53 loc) · 2.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from classes.Colour import Colour
from classes.FontInfo import FontInfo
import uuid
import time
from PIL import Image, ImageDraw
def loadTemplateImage(imageTemplate):
img = Image.open(imageTemplate)
img = img.convert("RGB")
return img
def generateNewFileName():
return str(uuid.uuid4())+".png"
def createNewImageFromTemplate(templateImage, randomColours, newFileName, ranges):
imageArray = templateImage.getdata()
colourCodes = [randomColours[i].colourCode for i in range(len(randomColours))]
newImage = [colourCodes[0] if item[0] in ranges[0] else colourCodes[1] if item[0] in ranges[1] else colourCodes[2] if item[0] in ranges[2] else item for item in imageArray]
templateImage.putdata(newImage)
templateImage.save(newFileName)
def addColourNamesToImage(randomColours, fontInfo, textLocations, newFileName):
image = Image.open(newFileName)
tmp = ImageDraw.Draw(image)
#this is definitely gonna break when you use different templates. but thats an issue for future me.
for index, item in enumerate(textLocations):
tmp.text(item, '"' + randomColours[index].colourName + '"', fill=fontInfo.fill, stroke_fill=fontInfo.strokeFill, stroke_width=fontInfo.strokeWidth, font=fontInfo.font)
image.save(newFileName)
#######-------------------------------------MAIN PROGRAM HERE----------------------------------------######
def __main__():
randomColours = []
textLocations = [(150,915), (150,500), (570,915)]
numberOfColours = 3
imageTemplate = "templates/3way_template.jpg"
saveFolder = "generated/"
if not os.path.exists(saveFolder):
os.makedirs(saveFolder)
newFileName = saveFolder + generateNewFileName()
fontInfo = FontInfo('fonts/Verdanai.ttf', 24)
fontInfo.fill = (250,250,250)
fontInfo.strokeFill = (50,50,50)
fontInfo.strokeWidth = 1
ranges = [range(20,30), range(30,100), range(100,200)]
img = loadTemplateImage(imageTemplate)
for n in range(numberOfColours):
colour = Colour()
colourCode = colour.generateRandomColour()
colourName = colour.getColourName(colourCode)
colour.colourCode = colourCode
colour.colourName = colourName
randomColours.append(colour)
createNewImageFromTemplate(img, randomColours, newFileName, ranges)
addColourNamesToImage(randomColours, fontInfo, textLocations, newFileName)
startTime = time.time()
__main__()
print("--- %s seconds ---" % (time.time() - startTime))