Skip to content

Commit ee85bb8

Browse files
committed
added convertToGrayscale() method; overlays seam
1 parent 2ad5f4d commit ee85bb8

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

SeamCarverUtilities.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import png, array
2+
13
def printVerticalSeam(sc):
24
"vertical seam is a list of cols"
35
seam = sc.findVerticalSeam()
@@ -39,4 +41,60 @@ def printVerticalSeamEnergy(sc):
3941
for col in range(sc.width()):
4042
if col == seam[row]:
4143
totalSeamEnergy += sc.energy(col, row)
42-
print "\nTotal seam energy: {:d}".format(totalSeamEnergy)
44+
print "\nTotal seam energy: {:d}".format(totalSeamEnergy)
45+
46+
def printHorizontalSeamEnergy(sc):
47+
"horizontal seam is a list of rows"
48+
seam = sc.findHorizontalSeam()
49+
totalSeamEnergy = 0
50+
for row in range(sc.height()):
51+
for col in range(sc.width()):
52+
if row == seam[col]:
53+
totalSeamEnergy += sc.energy(col, row)
54+
print "\nTotal seam energy: {:d}".format(totalSeamEnergy)
55+
56+
57+
def distToArray(sc):
58+
"displays distTo in matrix format"
59+
print "sc._distTo array\n"
60+
for r in range(sc.height()):
61+
for c in range(sc.width()):
62+
print '{:>8d}'.format(sc._distTo[r * sc.width() + c]),
63+
print
64+
65+
def _normalize(data):
66+
"normalize values in data collection"
67+
maxVal = max(data)
68+
normal_data = map(lambda x: x/float(maxVal), data)
69+
return normal_data
70+
71+
def convertToGrayscale(sc, filename_out, seam=None, horizontal=True):
72+
"converts sc.energy array to png image of grayscale values"
73+
w = png.Writer(width=sc.width(), height=sc.height(), bitdepth=8, greyscale=True)
74+
normal_energy = [int(round(x * 255)) for x in _normalize(sc._energy)]
75+
if seam:
76+
# write energy png with seam overlay
77+
_RED = 255
78+
if horizontal:
79+
# horizontal seam
80+
for col, row in enumerate(seam):
81+
index = row * sc.width() + col
82+
normal_energy[index] = _RED
83+
else:
84+
# vertical seam
85+
for row, col in enumerate(seam):
86+
index = row * sc.width() + col
87+
normal_energy[index] = _RED
88+
# write png
89+
90+
with open(filename_out, 'wb') as f:
91+
w.write_array(f, normal_energy)
92+
93+
94+
95+
96+
97+
98+
99+
100+

0 commit comments

Comments
 (0)