-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
133 lines (120 loc) · 4.74 KB
/
generate.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from PIL import Image
import json
from glob import glob
from collections import Counter
import random
ballot1 = Image.open("examples\\SampleBallot-1.png")
json1 = json.load(open("AZ-5-1.json", "r+"))
ballot2 = Image.open("examples\\SampleBallot-2.png")
json2 = json.load(open("AZ-5-2.json", "r+"))
redFiles = Counter(glob("primitives\\*R.png"))
accurateFiles = Counter(glob("primitives\\accurate*")) - Counter(redFiles)
badFiles = Counter(glob("primitives\\*")) - Counter(accurateFiles) - Counter(redFiles)
sections1 = json1["sections"]
sections2 = json2["sections"]
counter = 1
def setToString(set):
output = ''
for string in set:
output = output + "," + string
return output
def generate(file, color, extra, zero, bad_mark):
global counter
name1 = "test\\generatedballot-1-" + str(counter) + ".png"
name2 = "test\\generatedballot-2-" + str(counter) + ".png"
errorArray1 = set()
errorArray2 = set()
files = accurateFiles
if color:
errorArray1.add("bad color")
errorArray2.add("bad color")
files = files + redFiles
if bad_mark:
errorArray1.add("bad mark")
errorArray2.add("bad mark")
files = files + badFiles
fileList = list(files)
generatedballot1 = ballot1.copy().convert('RGBA')
for section in sections1:
bubbles = section["bubbles"]
max = section["max"]
if extra and zero:
extraRand = random.random() > .5
zeroRand = random.random() > .5
if zeroRand:
max = 0
errorArray1.add("blank section")
elif extraRand:
max = max + 1
errorArray1.add("extra bubble")
elif extra:
extraRand = random.random() > .5
if extraRand:
max = max + 1
errorArray1.add("extra bubble")
elif zero:
zeroRand = random.random() > .5
if zeroRand:
max = 0
errorArray1.add("blank section")
if bubbles is not None:
bubblesToUse = random.sample(bubbles, max)
for bubble in bubblesToUse:
shape = Image.open(fileList[random.randint(0, len(fileList) - 1)])
shape_x, shape_y = shape.size
center_x = bubble["TL_X"] + bubble["BR_X"]
center_y = bubble["TL_Y"] + bubble["BR_Y"]
generatedballot1.paste(shape, ((center_x - shape_x) // 2, (center_y - shape_y) // 2), mask=shape)
generatedballot2 = ballot2.copy().convert('RGBA')
for section in sections2:
bubbles = section["bubbles"]
max = section["max"]
if extra and zero:
extraRand = random.random() > .5
zeroRand = random.random() > .5
if zeroRand:
max = 0
errorArray2.add("blank section")
elif extraRand:
max = max + 1
errorArray2.add("extra bubble")
elif extra:
extraRand = random.random() > .5
if extraRand:
max = max + 1
errorArray2.add("extra bubble")
elif zero:
zeroRand = random.random() > .5
if zeroRand:
max = 0
errorArray2.add("blank section")
if bubbles is not None:
bubblesToUse = random.sample(bubbles, max)
for bubble in bubblesToUse:
shape = Image.open(fileList[random.randint(0, len(fileList) - 1)])
shape_x, shape_y = shape.size
center_x = bubble["TL_X"] + bubble["BR_X"]
center_y = bubble["TL_Y"] + bubble["BR_Y"]
generatedballot2.paste(shape, ((center_x - shape_x) // 2, (center_y - shape_y) // 2), mask=shape)
color1 = (random.randint(0,200),random.randint(0,200),random.randint(0,200))
color2 = (random.randint(0,200),random.randint(0,200),random.randint(0,200),)
generatedballot1 = generatedballot1.rotate(40*random.random()-20, Image.BICUBIC, expand=1, fillcolor=color1)
generatedballot2 = generatedballot2.rotate(40*random.random()-20, Image.BICUBIC, expand=1, fillcolor=color2)
bkgnd1 = Image.new("RGB", (3000, 4500), color1)
bkgnd2 = Image.new("RGB", (3000, 4500), color2)
bkgnd1.paste(generatedballot1, (100,100))
bkgnd2.paste(generatedballot2, (100,100))
bkgnd1.save(name1)
bkgnd2.save(name2)
file.write(name1+setToString(errorArray1)+"\n"+name2+setToString(errorArray2)+"\n")
counter = counter + 1
f = open("test\\test.txt", "w")
f.close()
f = open("test\\test.txt", "a+")
while counter <= 80:
color = random.random() > .5
extra = random.random() > .5
zero = random.random() > .5
bad_mark = random.random() > .5
generate(f, color, extra, zero, bad_mark)
f.close()