/
preProc.py
462 lines (439 loc) · 14.9 KB
/
preProc.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/python
import os
import time
import movieMaker
import remoji
from multiprocessing import Process, Pool
from collections import deque
import re
import spectrum
import pickle
mp4Bool = True
class Sequence:
def __init__(self, name):
self.name = name
self.framePaths = []
class GifInfo:
def __init__(self, name, mp4Bool, secondsRange):
self.name = name
self.framesDir = name + 'frames/'
movieMaker.wipeDir(self.framesDir)
if mp4Bool == False:
self.totFrames = remoji.convertGif(name, self.framesDir, auto = True)
else:
print 'totFrames getting created here'
self.totFrames = remoji.convertMp4(name, self.framesDir, secondsRange, auto = False)
def compressColor(color):
newColor = (color[0] / 4 * 4, color[1] / 4 * 4, color[2] / 4 * 4)
return newColor
# MAKES A MAP OF EVERY POSSIBLE COLOR
def buildColorMap(lilImgDir, mapFile):
st = time.time()
colorMap = {}
lilImgs = remoji.getLittleImgs(lilImgDir)
os.system('rm '+ mapFile)
f = open(mapFile, 'w+')
i = 0
# pool = Pool(processes = 4)
for r in range(0, 64):
for g in range(0, 64):
for b in range(0, 64):
closestImg = remoji.getClosestImg((r*4, g*4, b*4), lilImgs, colorMap, lilImgDir, oneColor = True)
#closestImg = pool.apply_async(remoji.getClosestImg, [(r, g, b), lilImgs, colorMap, lilImgDir, True]).get()
#f.write(str((r, g, b)) + ':' + closestImg + '\n')
i += 1
print i / float(262144)
nt = time.time()
print str(nt - st), 'seconds'
pickle.dump(colorMap, f)
# pool.close()
# pool.join()
#THIS FUNCTION LOADS AND RETURNS A COLOR MAP FROM A PREVIOUSLY WRITTEN TEXT FILE
def loadMapFile(mapFile):
f = open(mapFile)
colorMap = pickle.load(f)
return colorMap
# colorMap = {}
# i = 0
# for line in f:
# i+=1
# if i % 256 == 0:
# print i / float(256**3)
# sides = line.split(':')
# value = sides[1]
# nums = getNums(sides[0])
# color = (nums[0], nums[1], nums[2])
# colorMap[(color, 'emoji/')] = value[:-1]
# print colorMap
# print 'finished loading colorMap'
# return colorMap
#THIS FUNCTION TAKES A LINE, AND SPLITS IT UP INTO WORDS BY WHITE SPACE
def getWords(line):
lineWords = re.split('\s+', line)
#print lineWords
while lineWords[-1] is '':
lineWords.pop()
if len(lineWords) == 0:
return None
while lineWords[0] is '':
lineWords.pop(0)
if len(lineWords) == 0:
return None
return lineWords
def getNums(line):
lineNums = re.split('\D+', line)
while lineNums[-1] is '':
lineNums.pop()
if len(lineNums) == 0:
return None
while lineNums[0] is '':
lineNums.pop(0)
if len(lineNums) == 0:
return None
lineNums = [ int(x) for x in lineNums]
return lineNums
#THIS FUNCTION POPULATES FRAMEMAP GIVEN INSTRUCTIONS FOR A GIVE SEQUENCE SUPPLIED IN THE LIST ANIMS
#ANIMS IN THE FORMAT: anim = (gifName, lilImgDir, FRAMES, SPEED, SRES, ERES (OPTIONAL))
#THIS FUNCTION ALSO BUILDS THE GIVEN SEQUENCE'S FRAMEPATHS LIST
def getMosFrames(anims, colorMap, gifMap, frameMap, movDir, outputName, seq, dbFrame, loopFrame, specDirOrder = None, secondsRange = (0, 60)):
# frame = 1
# loopFrame = 0
j = loopFrame
for anim in anims:
if len(anim) is 6:
#print 'anim: ', anim
[gifName, lilImgDir, frames, speed, sRes, eRes] = anim
if len(anim) is 5:
[gifName, lilImgDir, frames, speed, sRes] = anim
eRes = sRes
#print 'frames: ', frames
frames = int(frames)
speed = int(speed)
sRes = int(sRes)
eRes = int(eRes)
framesPerGif = None
#print 'gifMap: ', gifMap
#print 'gifMap: ', gifMap
if gifName in gifMap:
if mp4Bool == False:
framesPerGif = gifMap[gifName].totFrames
else:
framesPerGif = (secondsRange[1] - secondsRange[0]) * 30
print 'framesPerGif:i',framesPerGif
else:
gifInfo = GifInfo(gifName, mp4Bool, secondsRange)
gifMap[gifName] = gifInfo
print 'mp4Bool: ' + str(mp4Bool)
if mp4Bool == False:
framesPerGif = gifInfo.totFrames
else:
framesPerGif = (secondsRange[1] - secondsRange[0]) * 30
approxRes = sRes
approxStep = (eRes - sRes) / (float(frames * speed))
if specDirOrder != None:
#print 'specDirOrder:', specDirOrder
#print 'len(specDirOrder):', len(specDirOrder)
# newAnims is a longer version of anims to account for increased directory changes in (spec) mode
newAnims = []
curAnimCounter = 0
for i in range(0, int(frames)):
curRes = int(round(approxRes))
curAnim = anims[curAnimCounter]
#print 'curAnim:', curAnim
curSpecDir = specDirOrder.pop(0)
newAnims.append([gifName, curSpecDir, 1, speed, approxRes])
approxRes += approxStep
# Recursive call to getMosFrames with each anim from newAnims
[dbFrame, loopFrame] = getMosFrames(newAnims, colorMap, gifMap, frameMap, movDir, outputName, seq, dbFrame, loopFrame, secondsRange = secondsRange)
########
else:
for i in range(0, int(frames)):
for s in range(0, int(speed)):
curRes = int(round(approxRes))
dbFrameStr = movieMaker.getFrameStr(dbFrame, 4)
print dbFrameStr
frameDesc = (gifName, lilImgDir, j % framesPerGif, curRes)
if frameDesc in frameMap:
print 'appending ', frameDesc, 'to frameMap'
seq.framePaths.append(frameMap[frameDesc])
#print frameDesc, ' already in frameMap'
else:
frameMap[frameDesc] = 'unique/' + outputName + dbFrameStr + '.png'
#print frameDesc, ':', frameMap[frameDesc]
seq.framePaths.append(frameMap[frameDesc])
dbFrame += 1
approxRes += approxStep
j += 1
loopFrame = j
return [dbFrame, loopFrame]
#THIS FUNCTION IS CALLED WHEN READFILE IS READING A SEQUENCE WITH THE 'SPEC' OPTION. IT USES THE ANIMS STARTING WITH NUMBERS TO CALCULATE THE 2D GRADIENTS AND THE RATE AT WHICH THE COLOR PALETTE CHANGES. THEN IT MAKES TEMPORARY DIRECTORIES FOR IMAGES CONTAINED IN SPECTRUMS, AND THESE DIRECTORIES ARE ASSIGNED TO EACH ANIM LINES 'LILIMGDIR' SLOT ACCORDINGLY. THIS FUNCTION ALSO HELPS BUILD SPECMAP
def modifySpecAnims(anims, colorMap, whiteSquare, lilImgDBDir = 'emoji/'):
littleImgs = remoji.getLittleImgs(lilImgDBDir)
newAnims = []
i = 0
j = 0 #Runner
curSpec = None
specDirOrder = [] #DIRECTORY NAMES IN ORDER CORRESPONDING 1:1 TO FRAMES IN THE ANIMATION
#if 'spec' not os.path.isdir('../spec'):
# if not os.path.isdir('../spec'):
# movieMaker.wipeDir('spec/')
while i < len(anims):
# if type(anims[i][0]) == int:
if curSpec == None:
pivotColors = []
steps = anims[i].pop()
#print 'anims[' + str(i) + ']: ', anims[i]
for p in range(0, len(anims[i]) / 3):
r = int(anims[i].pop(0))
g = int(anims[i].pop(0))
b = int(anims[i].pop(0))
tempPivotColor = []
tempPivotColor.append(r)
tempPivotColor.append(g)
tempPivotColor.append(b)
pivotColors.append(tempPivotColor)
curSpec = spectrum.Spectrum(steps, pivotColors, lilImgDBDir, littleImgs, colorMap, whiteSquare)
#print curSpec.levelsPerColor, curSpec.pivotColors
j = i + 1
#JUMP TO NEXT SPECTRUM
while type(anims[j][0]) != int:
j += 1
steps = int(anims[j].pop())
pivotColors = []
for p in range(0, len(anims[j]) / 3):
#print 'p:',p
#print len(anims[j])
r = int(anims[j].pop(0))
g = int(anims[j].pop(0))
b = int(anims[j].pop(0))
tempPivotColor = []
tempPivotColor.append(r)
tempPivotColor.append(g)
tempPivotColor.append(b)
pivotColors.append(tempPivotColor)
#print 'pivotColors: ', pivotColors
nextSpec = spectrum.Spectrum(steps, pivotColors, lilImgDBDir, littleImgs, colorMap, whiteSquare)
#print type(nextSpec), type(curSpec)
spec2d = spectrum.make2dSpectrum(20, curSpec, nextSpec, lilImgDBDir, littleImgs, colorMap)
transitionFrames = 0
tempNonSpecAnims = []
#COUNT TOTAL TRANSITION FRAMES IN THIS LOOP
while i < (j - 1):
i += 1
#print i, anims[i]
newAnims.append(anims[i])
tempNonSpecAnims.append(anims[i])
moreFrames = int(anims[i][2])
transitionFrames += moreFrames
i = j
totSpectrums = len(spec2d) - 1 # -1 so that last spectrum doesnt get re-used in next anim
floatStep = totSpectrums / float(transitionFrames)
specDirShortOrder = []
for frame in range(0, transitionFrames):
approxStep = floatStep * frame
roundStep = int(round(approxStep))
spec = spec2d[roundStep]
dirName = spec.namify()
specDirShortOrder.append(dirName)
#IF THERES A PATH TO SPEC/DIRNAME, DONT BOTHER, ELSE, MAKE IT
#specDirs = os.listdir('spec/')
#if dirName[:-1] not in specDirs:
# os.system('mkdir spec/' + dirName[:-1])
# #print 'copyingImgsToDir ..'
# spec.copyImgsToDir('spec/' + dirName)
movieMaker.wipeDir('spec/' + dirName)
spec.copyImgsToDir('spec/' + dirName)
specDirOrder.append('spec/' + dirName)
#print specDirs
#print dirName
# print 'specDirOrder:', specDirOrder
if j == (len(anims) - 1):
break
curSpec = nextSpec
return [newAnims, specDirOrder]
def readFile(instructionsFile, movDir, outputName, colorMap = {}, mp4Bool = False, secondsRange = (0, 60)):
movieMaker.wipeDir('unique/')
frameMap = {}
specMap = {} # { (PIVOTCOLORS, LEVELSPERPIVOTCOLOR, LILIMGDIR) : SPECTRUMDIR }
# colorMap = {} # { (COLOR TUPLE, LILIMGDIR) : LILIMGPATH }
definedSequences = {}
lilImgMap = {}
gifMap = {}
seqOrder = []
dbFrame = 1
f = open(instructionsFile)
frame = 1
lines = f.readlines()
curLine = 0
#EVENTUALLY CHANGE THIS TO ROLLING I VALUE
while curLine < len(lines):
lineWords = getWords(lines[curLine])
if lineWords is None:
curLine += 1
continue
if '#' in lineWords[0]:
curLine += 1
continue
if lineWords[0] == 'Sequence':
seqLines = []
seqName = lineWords[1]
seqType = lineWords[2][1:-1]
seq = Sequence(seqName)
#SPECTRUM MODE LINE PARSING
if seqType == 'spec':
lilImgDir = ''
baseDir = lineWords[3]
#print lineWords
loopFrame = int(lineWords[4])
whiteSquare = False
if baseDir[-1] == 'w':
baseDir = baseDir[:-1]
whiteSquare = True
anims = []
j = curLine + 1
while getWords(lines[j])[0] != 'endSeq':
seqLines.append(lines[j])
if '#' in lines[j]:
curLine += 1
continue
j += 1
seqLines.append(lines[j])
curSeqLine = 0
while curSeqLine < len(seqLines):
s = re.search('^\t{1}\S', seqLines[curSeqLine])
try:
match = s.group(0)
except:
s = re.search('^\t{2}\S', seqLines[curSeqLine])
try:
match = s.group(0)
except:
s = re.search('^\t{3}\S', seqLines[curSeqLine])
try:
match = s.group(0)
seqLineWords = getWords(seqLines[curSeqLine])
if len(seqLineWords) is 4:
anim = [gifName, lilImgDir, seqLineWords[0], seqLineWords[1], seqLineWords[2], seqLineWords[3]]
#print anim
anims.append(anim)
if len(seqLineWords) is 3:
anim = [gifName, lilImgDir, seqLineWords[0], seqLineWords[1], seqLineWords[2]]
anims.append(anim)
except:
curSeqLine += 1
continue
curSeqLine += 1
continue
gifName = getWords(seqLines[curSeqLine])[0]
curSeqLine += 1
continue
spectrumNums = getNums(seqLines[curSeqLine])
anims.append(spectrumNums)
curSeqLine += 1
print 'anims:', anims
(anims, specDirOrder) = modifySpecAnims(anims, colorMap, whiteSquare, baseDir)
#print 'anims:',anims
#print 'len(anims):',len(anims)
#print 'specDirOrder:',specDirOrder
#print 'len(specDirOrder):',len(specDirOrder)
[dbFrame, loopFrame] = getMosFrames(anims, colorMap, gifMap, frameMap, movDir, outputName, seq, dbFrame, loopFrame, specDirOrder, secondsRange = secondsRange)
#MOSAIC MODE LINE PARSING
if seqType == 'mos':
#ANIMS DESCRIBE SEGMENTS OF THE SEQUENCE
loopFrame = int(lineWords[3])
anims = []
j = curLine + 1
while getWords(lines[j])[0] != 'endSeq':
seqLines.append(lines[j])
if '#' in lines[j]:
curLine += 1
continue
j += 1
# print seqLines
seqLines.append(lines[j])
curSeqLine = 0
while curSeqLine < len(seqLines):
#print 'seqLines[curSeqLines]:', seqLines[curSeqLine]
#MATCH SINGLE TABBED LINE
s = re.search('^\t{1}\S', seqLines[curSeqLine])
try:
match = s.group(0)
except:
#MATCH DOUBLE TABBED LINE
s = re.search('^\t{2}\S', seqLines[curSeqLine])
try:
match = s.group(0)
seqLineWords = getWords(seqLines[curSeqLine])
if len(seqLineWords) is 4:
anim = (gifName, lilImgDir, seqLineWords[0], seqLineWords[1], seqLineWords[2], seqLineWords[3])
anims.append(anim)
if len(seqLineWords) is 3:
anim = (gifName, lilImgDir, seqLineWords[0], seqLineWords[1], seqLineWords[2])
anims.append(anim)
except:
curSeqLine += 1
continue
curSeqLine += 1
continue
# print 'match: ',match
seqLineWords = getWords(seqLines[curSeqLine])
(gifName, lilImgDir) = (seqLineWords[0], seqLineWords[1])
#print 'gifName, lilImgDir): ', (gifName, lilImgDir)
curSeqLine += 1
[dbFrame, loopFrame] = getMosFrames(anims, colorMap, gifMap, frameMap, movDir, outputName, seq, dbFrame, loopFrame, secondsRange = secondsRange)
definedSequences[seq.name] = seq
if lineWords[0] == 'makeAnim':
j = curLine + 1
while getWords(lines[j])[0] != 'endAnim':
if '#' in lines[j]:
curLine += 1
continue
seqOrder.append(getWords(lines[j])[0])
j += 1
#print seqOrder
frame = 1
pool = Pool(processes = 4)
inputFrameNames = []
# print frameMap[('tunnel2', 'animalmini2/', 1, 17)]
print 'making mosaics for ' + str(len(frameMap)) + ' unique frames...'
for key in frameMap:
#print 'key: ', key
#print 'frameMap[key]: ', frameMap[key]
(gifName, lilImgDir, loopFrame, curRes) = key
if gifName not in gifMap:
gifInfo = GifInfo(gifName, mp4Bool, secondsRange)
print 'new gifInfo class'
gifMap[gifName] = gifInfo
if lilImgDir not in lilImgMap:
#print lilImgDir
littleImgs = remoji.getLittleImgs(lilImgDir)
lilImgMap[lilImgDir] = littleImgs
else:
littleImgs = lilImgMap[lilImgDir]
# littleImgs = remoji.getLittleImgs(lilImgDir)
for key in frameMap:
(gifName, lilImgDir, loopFrame, curRes) = key
inputFrameNames = os.listdir(gifMap[gifName].framesDir)
inputFrameNames.sort()
depthPix = curRes + 6
# print ['unique/' + inputFrameNames[loopFrame], 'autoScale', depthPix, littleImgs, frameMap[key], 'arbitraryDir/']
#print 'lilImgMap:,',lilImgMap
#remoji.makeMosaic(gifMap[gifName].framesDir + inputFrameNames[loopFrame], 'autoScale', depthPix, lilImgMap[lilImgDir], frameMap[key], colorMap, lilImgDir, 'arbitraryDir/')
#print gifMap[gifName]
#print inputFrameNames
#print loopFrame
#print inputFrameNames[loopFrame]
#print lilImgMap[lilImgDir]
#print frameMap[key]
pool.apply_async(remoji.makeMosaic, [gifMap[gifName].framesDir + inputFrameNames[loopFrame], 'autoScale', depthPix, lilImgMap[lilImgDir], frameMap[key], colorMap, lilImgDir])
pool.close()
pool.join()
movieMaker.wipeDir(movDir)
for seqName in seqOrder:
seq = definedSequences[seqName]
for framePath in seq.framePaths:
frameStr = movieMaker.getFrameStr(frame, 4)
os.system('cp ' + framePath + ' ' + movDir + outputName + frameStr + '.png')
frame += 1
break
curLine += 1