-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_png.py
executable file
·164 lines (141 loc) · 4.63 KB
/
to_png.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
#!/usr/bin/env python
"""
This program converts all .h5 files in the current
directory to .png using the HAWK program
image_to_png
"""
import os, re, sys, spimage
def read_files():
l = os.listdir('.')
files = [f for f in l if re.search('.h5$',f)]
return files
def evaluate_arguments(arguments):
if len(arguments) <= 0:
print """
This program converts all h5 files in the curren directory to png.
Usage: python_script_to_png [colorscale]
Colorscales:
Jet
Gray
PosNeg
InvertedPosNeg
Phase
InvertedPhase
Log (can be combined with the others)
Shift (can be combined with the others)
Support
"""
return
elif not (isinstance(arguments,list) or isinstance(arguments,tuple)):
print "function to_png takes must have a list or string input"
return
log_flag = 0
shift_flag = 0
support_flag = 0
color = 16
for flag in arguments:
if flag == 'PosNeg':
color = 8192
elif flag == 'InvertedPosNeg':
color = 16384
elif flag == 'Phase':
color = 256
elif flag == 'InvertedPhase':
color = 4096
elif flag == 'Jet':
color = 16
elif flag == 'Gray':
color = 1
elif flag == 'Log':
log_flag = 1
elif flag == 'Shift':
shift_flag = 1
elif flag == 'Support':
support_flag = 1
else:
print "unknown flag %s" % flag
if log_flag == 1:
color += 128
return color,shift_flag,support_flag
def get_shift_function(bool):
if bool:
def shift_function(img):
ret = spimage.sp_image_shift(img)
spimage.sp_image_free(img)
return ret
else:
def shift_function(img):
return img
return shift_function
def get_support_function(bool):
if bool:
def support_function(img):
spimage.sp_image_mask_to_image(img,img)
else:
def support_function(img):
pass
return support_function
def to_png_parallel(*arguments):
import multiprocessing
import Queue
class Worker(multiprocessing.Process):
def __init__(self, working_queue, process_function, color):
multiprocessing.Process.__init__(self)
self.working_queue = working_queue
self.process_function = process_function
self.color = color
def process(self, f):
#for f in files:
img = spimage.sp_image_read(f,0)
img = self.process_function(img)
spimage.sp_image_write(img,f[:-2]+"png",self.color)
spimage.sp_image_free(img)
def run(self):
while not self.working_queue.empty():
print "%s get new, approx %d left" % (self.name,self.working_queue.qsize())
try:
#f = self.working_queue.get_nowait()
f = self.working_queue.get()
except Queue.Empty:
break
self.process(f)
print "%s done" % self.name
def split_files(files,n):
from pylab import split, array
rest = len(files)%n
if rest:
super_list = split(array(files)[:-rest],len(files)/n)
super_list.append(files[-rest:])
else:
super_list = split(array(files),len(files)/n)
return super_list
def run_threads(arguments,nThreads):
color,shift_flag,support_flag = evaluate_arguments(arguments)
shift_function = get_shift_function(shift_flag)
support_function = get_support_function(support_flag)
def process_function(img):
support_function(img)
return shift_function(img)
files = read_files()
#super_list = split_files(files,10)
working_queue = multiprocessing.Queue()
# for job in super_list:
# working_queue.put(job)
for f in files:
working_queue.put(f)
for i in range(nThreads):
Worker(working_queue, process_function, color).start()
run_threads(arguments,multiprocessing.cpu_count())
def to_png(*arguments):
color,shift_flag,support_flag = evaluate_arguments(arguments)
files = read_files()
shift_function = get_shift_function(shift_flag)
support_function = get_support_function(support_flag)
for f in files:
img = spimage.sp_image_read(f,0)
support_function(img)
img = sp_image_shift(img)
spimage.sp_image_write(img,f[:-2]+"png",color)
spimage.sp_image_free(img)
if __name__ == "__main__":
to_png_parallel(*sys.argv[1:])