-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
94 lines (90 loc) · 4.06 KB
/
preprocess.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
import cv2
import itertools
from os import path
from sys import argv
from compress_pickle import dump
from termcolor import colored
import functions as fun
import settings as sel
if fun.isNotebook():
(fPath, fName) = ('./demo', 'awoofySquare.png')
else:
(fPath, fName) = (argv[1], argv[2])
VERBOSE = True
###############################################################################
# Load image
###############################################################################
img = cv2.imread(path.join(fPath, fName.split(".png")[0]+'_DWN.png'))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
###############################################################################
# Process image for colors
###############################################################################
pixs = tuple([tuple([tuple(i) for i in row]) for row in img])
colPal = list(set(list(itertools.chain(*pixs))))
###############################################################################
# Change colors into keys
###############################################################################
colDict = {col: ix for (ix, col) in enumerate(colPal)}
colDeDict = {ix: col for (ix, col) in enumerate(colPal)}
pixDict = tuple([tuple([colDict[i] for i in row]) for row in pixs])
###############################################################################
# Get run length
###############################################################################
dictVals = list(colDict.values())
(pVectors, pLengths) = fun.getVectorCounts(pixDict, dictVals)
###############################################################################
# Assemble and export vectors
# -----------------------------------------------------------------------------
# colorMapper: RGB tuple to color-index mapper dictionary
# colorDeMapper: Color-index to RGB mapper dictionary
# imageMapped: Color-index pixel representation of the original image
# runLengthVectors: Run-length encoded vectors by color
# runLengthLengths: Run-length lengths per color
###############################################################################
pDict = {
'colorMapper': colDict,
'colorDeMapper': colDeDict,
'runLengthVectors': pVectors,
'runLengthLengths': pLengths,
'imageMapped': pixDict
}
pklFName = path.join(fPath, fName.split('.png')[0])+'.pkl'
dump(pDict, pklFName)
###############################################################################
# Terminal Summary
###############################################################################
pSums = {i: sum(pVectors[i]) for i in pVectors.keys()}
if VERBOSE:
print(colored(f'+ Vector lengths: {pSums}', 'blue'))
###############################################################################
# Split longer entries
###############################################################################
if sel.USER_SEL['lengthMax'] is not None:
(colDict, colDeDict, scrambler) = fun.genScrambleDicts(
pDict, threshold=sel.USER_SEL['lengthMax']
)
if sel.USER_SEL['shuffler'] == 'shuffler':
print(colored("- Using Uniform Scrambler", 'red'))
pixDict = fun.scramblePixDictUniform(pixDict, scrambler)
else:
print(colored("- Using Length-Based Scrambler", 'red'))
pixDict = fun.scramblePixDictLength(
pixDict, scrambler, intRange=sel.USER_SEL['shuffleRange']
)
# Re-encode ---------------------------------------------------------------
dictVals = sorted(fun.flatten(scrambler.values()))
(pVectors, pLengths) = fun.getVectorCounts(pixDict, dictVals)
# Setup new dictionary ----------------------------------------------------
pDict = {
'colorMapper': colDict,
'colorDeMapper': colDeDict,
'runLengthVectors': pVectors,
'runLengthLengths': pLengths,
'imageMapped': pixDict
}
pklFName = path.join(fPath, fName.split('.png')[0])+'.pkl'
dump(pDict, pklFName)
# Terminal Summary --------------------------------------------------------
pSums = {i: sum(pVectors[i]) for i in pVectors.keys()}
if VERBOSE:
print(colored(f'+ Split Vector lengths: {pSums}', 'blue'))