-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrgbcsv2rgbarray.py
92 lines (83 loc) · 3.07 KB
/
rgbcsv2rgbarray.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
#!/usr/bin/env python
# rgbcvs2rgbarray.py
# by [email protected]
# Usage: rgbcvs2colorarray.py rgb_combined.csv
# Oputput: A = array([[222,43,221],[2,11,222], ... ])
# Open resulting file rgb_colorarray.py and paste into rgb2colorname.py
from __future__ import print_function
# Begin timer:
import timeit
start_time = timeit.default_timer()
# Get argument list using sys module:
import sys, os.path
def program(*args):
# do whatever
pass
# Provide default file_in name argument if not provided:
if __name__ == "__main__":
#def main(argv):
try:
arg1 = sys.argv[1]
file_in = sys.argv[1]
except IndexError: # getopt.GetoptError:
# print "Usage: " + sys.argv[0] + ' -i <inputfile> -o <outputfile>'
# sys.exit(2)
file_in = 'rgb_combined_v05.csv'
# Exit if file_in not found:
if os.path.exists(file_in) and os.access(file_in, os.R_OK):
import csv, time
with open(file_in, 'rU') as f:
reader = csv.reader(f, delimiter=',')
for i in reader:
header_rows = '# '+time.strftime('%Y-%m-%d %H:%M (local time)')+" "+sys.argv[0]+' START: outrowcount='+ str( sum(1 for _ in f) ) +'.'
print(header_rows)
else:
print('# '+time.strftime('%Y-%m-%d %H:%M (local time)')+' '+sys.argv[0]+" ABORTED. Either file "+file_in+" is missing or is not readable.")
exit(2)
# Provide default file_out name argument if not provided:
if __name__ == "__main__": #def main(argv):
# Name output file by appending .txt to the name:
try:
arg1 = sys.argv[2]
file_out = sys.argv[2] +'.'+ file_in + '.txt'
except IndexError: # getopt.GetoptError:
file_out = file_in + '.txt'
# Send STDOUT to a file:
stdout = sys.stdout # remember the handle to the real standard output.
sys.stdout=open( file_out,"w")
# Print in yml format:
import csv
# 'rU' means open in universal-newline mode needed on Macs:
#with open('./Portfolio.csv', 'rU') as f:
with open(file_in, 'rU') as f:
reader = csv.reader(f, delimiter=',')
first_line = f.readline() # pull out first line - do not print
# TODO: Remove duplicates.
print(" RGB = np.array([",end="")
# Loop through lines in input to generate: "[222,43,221],[2,11,222]", one for each color:
rownum=0
for i in reader:
strRGB='['+i[1]+','+i[2]+','+i[3]+']'
if rownum ==0:
# print first row without a comma:
print(strRGB,end="")
lastRGB=strRGB
rownum=rownum+1
else:
if lastRGB == strRGB:
# skip the duplicate
pass
else:
print(','+strRGB,end="")
lastRGB = strRGB
rownum=rownum+1
print("])") # with NewLine
footer_rows="# "+ time.strftime('%Y-%m-%d %H:%M (local time)') +' '+ sys.argv[0] +' '+ file_out+" output "+ str(rownum)+ ' rows.'
print(' '+footer_rows)
# Close the file every time:
sys.stdout.close()
sys.stdout = stdout # Restore regular stdout.
# End timer:
elapsed = timeit.default_timer() - start_time
print("# "+ time.strftime('%Y-%m-%d %H:%M (local time)') +' '+ sys.argv[0] +" END: ran for "+ "{:.2f}".format(elapsed * 1000 )+ ' secs.')
print(footer_rows)