-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgbcsv2colorarray.py
111 lines (95 loc) · 2.59 KB
/
rgbcsv2colorarray.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
# by 1951FDG, Wilson Mar
from __future__ import print_function
import csv
import os.path
import sys
import time
import timeit
# Begin timer:
start_time = timeit.default_timer()
def program(*args):
# do whatever
pass
# Provide default file_in name argument if not provided:
if __name__ == "__main__":
# def main(argv):
try:
file_in = sys.argv[1]
except IndexError: # getopt.GetoptError:
print("Usage: " + sys.argv[0] + " -i <inputfile> -o <outputfile>")
sys.exit(2)
# Exit if file_in not found:
if os.path.exists(file_in) and os.access(file_in, os.R_OK):
with open(file_in, newline="") 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: rowcount="
+ 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."
)
sys.exit()
# Provide default file_out name argument if not provided:
if __name__ == "__main__":
# def main(argv):
try:
file_out = sys.argv[2]
except IndexError: # getopt.GetoptError:
# Name output file by appending .txt to the name:
file_out = sys.argv[0] + ".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 black format:
with open(file_in, newline="") as in_f:
reader = csv.DictReader(in_f, delimiter=",")
print("RGB = np.array(")
print(" " + "[")
rownum = 0
for i in reader:
print(" " + " " + "[" + i["_Red"] + ", " + i["_Green"] + ", " + i["_Blue"] + "]" + ",")
rownum = rownum + 1
print(" " + "]")
print(")")
footer_rows = (
"# "
+ time.strftime("%Y-%m-%d")
+ " "
+ os.path.basename(sys.argv[0])
+ " "
+ os.path.basename(file_out)
+ " output "
+ str(rownum)
+ " rows."
)
print(footer_rows, end="") # no NewLine
# 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)