-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl14_wire_spiral.py
More file actions
149 lines (120 loc) · 4.12 KB
/
l14_wire_spiral.py
File metadata and controls
149 lines (120 loc) · 4.12 KB
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
import PIL
from PIL import Image
def resizeImage(fni, fno, size):
im = Image.open(fni)
im = im.resize(size)
im.save(fno)
def makeSpiral(pixdata, width, height):
top = 0
right = width-1
bottom = width*height-1
left = bottom-width+1
print "width, height :", width, height
print "top", top, "| right", right, "| bottom", bottom, "| left", left
l = pixdata
lnew = []
while (top <= right):
## go left-right. use top to indicate start!!
lnew.extend(l[top : right+1 : 1]) ## make sure to INCLUDE right
## next "top" will be one line down, one pixel right
top += width
top += 1
if (right >= bottom):
break
## go top-bottom. use right+width to indicate start!!
lnew.extend(l[right+width : bottom+1 : width]) ## make sure to INCLUDE bottom
## next "right" will be one line down, one pixel left
right += width
right -= 1
if (left >= bottom):
break
## go right-left. use bottom-1 to indicate start !!
lnew.extend(l[bottom-1 : left-1 : -1]) ## make sure to INCLUDE left
## next "bottom" will be one line up, one pixel left
bottom -= width
bottom -= 1
if (top >= left):
break
## go bottom-top. use left-width to indicate start!!
lnew.extend(l[left-width : top-width : -width]) ## make sure to INCLUDE top
## next "left" will be one line up, one pixel right
left -= width
left += 1
return lnew
def makeUnSpiral(pixdata, width, height):
top = 0
right = width-1
bottom = width*height-1
left = bottom-width+1
print "width, height :", width, height
print "top", top, "| right", right, "| bottom", bottom, "| left", left
l = pixdata
lnew = [0] * len(pixdata) ## new pic should have same length
cnt = 0
while (top <= right):
## go left-right. use top to indicate start!!
## make sure to INCLUDE right
for i in range(top, right+1, 1):
lnew[i] = l[cnt]
cnt += 1
## next "top" will be one line down, one pixel right
top += width
top += 1
if (right >= bottom):
break
## go top-bottom. use right+width to indicate start!!
## make sure to INCLUDE bottom
for i in range(right+width, bottom+1, width):
lnew[i] = l[cnt]
cnt += 1
## next "right" will be one line down, one pixel left
right += width
right -= 1
if (left >= bottom):
break
## go right-left. use bottom-1 to indicate start !!
## make sure to INCLUDE left
for i in range(bottom-1, left-1, -1):
lnew[i] = l[cnt]
cnt += 1
## next "bottom" will be one line up, one pixel left
bottom -= width
bottom -= 1
if (top >= left):
break
## go bottom-top. use left-width to indicate start!!
## make sure to INCLUDE top
for i in range(left-width, top-width, -width):
lnew[i] = l[cnt]
cnt += 1
## next "left" will be one line up, one pixel right
left -= width
left += 1
print "final cnt is at", cnt
return lnew
def runTests():
for i in range(1,6):
for j in range(1,6):
width, height = i, j
print "*" * 40
print "*** Doing width, height", width, ",", height, " ***"
print "*" * 40
l = range(width*height)
print "*** spiralling ..."
lnew = makeSpiral(l, width, height)
print "length of spiraled pic =", len(lnew)
print lnew
print
print "*** un-spiralling ..."
lo = makeUnSpiral(lnew, width, height)
print "length of unspiraled pic =", len(lo)
print lo
assert l == lo
##runTests()
f = Image.open("C:\\wire.png")
l = list(f.getdata())
width, height = 100, 100
lnew = makeUnSpiral(l, width, height)
g = Image.new("RGB", (width, height))
g.putdata(lnew)
g.save("C:\\aha_un_spiral" + str(height) + ".png")