-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Dbar_photo.py
102 lines (92 loc) · 3.77 KB
/
3Dbar_photo.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
import sys
import random
def parse_image(fileName):
rows = 0
cols = 0
image = []
with open(fileName, 'r') as file:
file.readline()
dimensions = file.readline().strip().split()
rows = int(dimensions[0])
cols = int(dimensions[1])
maxPixel = file.readline()
for i in range(rows):
image.append([])
for j in range(cols):
image[i].append(int(file.readline().strip()))
return image, rows, cols
def make_bars(maxX, maxY, x, y, color):
output = ""
sw = 4
if x > maxX - 0.07*maxX and x < maxX + 0.07*maxX and y > maxY - 0.07*maxY and y < maxY + 0.07*maxY:
#x and y both close to axis
output += make_line(x, y, sw, color, "square")
elif x > maxX/2 - 0.07*maxX/2 and x < maxX/2 + 0.07*maxX/2:
# only x close to axis
m = make_m(y, maxY/2)
if y > maxY/2:
output += make_line(x, y, sw, color, "above", m)
else:
output += make_line(x, y, sw, color, "below", m)
elif y > maxY/2 - 0.07*maxY/2 and y < maxY/2 + 0.07*maxY/2:
# only x close to axis
m = make_m(x, maxX/2)
if x > maxX/2:
output += make_line(x, y, sw, color, "right", m)
else:
output += make_line(x, y, sw, color, "left", m)
else:
# neither
m1 = make_m(x, maxX/2)
m2 = make_m(y, maxY/2)
if x > maxX/2:
if y > maxY/2:
output += make_line(x, y, sw, color, "top_right", m1, m2)
else:
output += make_line(x, y, sw, color, "bottom_right", m1, m2)
else:
if y > maxY/2:
output += make_line(x, y, sw, color, "top_left", m1, m2)
else:
output += make_line(x, y, sw, color, "bottom_left", m1, m2)
return output
def make_m(val, maxVal):
if val < maxVal:
return 8*(maxVal - val)/maxVal
else:
return 8*(val - maxVal)/maxVal
def make_line(x, y, sw, color, function, m1 = 0, m2 = 0):
if function == "square":
return str(x) + " " + str(y) + " " + str(sw) + " " + str(color) + " square\n"
if function == "above" or function == "below" or function == "left" or function == "right":
return str(x) + " " + str(y) + " " + str(sw * 0.93) + " " + str(sw) + " " + str(m1) + " " + str(color) + " " + function + "\n"
if function == "top_left" or function == "top_right" or function == "bottom_left" or function == "bottom_right":
return str(x) + " " + str(y) + " " + str(m1) + " " + str(m2) + " " + str(sw * 0.93) + " " + str(sw) + " " + str(color) + " " + function + "\n"
def main():
fileName = 'output.eps'
rules = ""
imageFile = sys.argv[1]
with open('3Dbar_functions.txt', 'r') as file:
rules_list = file.readlines()
for line in rules_list:
rules += line
rules += '\n\n'
image, maxX, maxY = parse_image(imageFile)
sw = 8
with open(fileName, 'w') as file:
file.write('%!PS-Adobe-3.0 EPSF-3.0\n')
file.write('%%BoundingBox: 0 0 ' + str(sw*maxX) + ' ' + str(sw*maxY) + '\n\n')
file.write(rules)
on = 255
for i in range(maxX):
for j in range(maxY):
x = sw + sw*j
y = sw + sw*(maxX-1-i)
if image[i][j] < 100: #only print at pixels < 100
if image[i][j] < 75: #fill in black for darkest pixels < 75
file.write(make_bars(sw*maxX, sw*maxY, x, y, 0))
else:
file.write(make_bars(sw*maxX, sw*maxY, x, y, 255)) #fill in white for pixels > 75
#file.write(make_bars(sw*maxX, sw*maxY, x, y, (image[i][j]/255)/0.3)) #for grey scale
file.write('showpage')
main()