1
+ import png , array
2
+
1
3
def printVerticalSeam (sc ):
2
4
"vertical seam is a list of cols"
3
5
seam = sc .findVerticalSeam ()
@@ -39,4 +41,60 @@ def printVerticalSeamEnergy(sc):
39
41
for col in range (sc .width ()):
40
42
if col == seam [row ]:
41
43
totalSeamEnergy += sc .energy (col , row )
42
- print "\n Total seam energy: {:d}" .format (totalSeamEnergy )
44
+ print "\n Total 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 "\n Total 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