-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch14.py
More file actions
94 lines (80 loc) · 1.78 KB
/
ch14.py
File metadata and controls
94 lines (80 loc) · 1.78 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
from PIL import Image as im
def init_row():
ret = []
for i in range(100):
ret.append([]) # each list holds a pixel
return ret
def init():
ret = []
for i in range(100):
ret.append(init_row()) #each list is a row in the final image
return ret
def split(li):
ret = []
for elem in li:
ret.append([elem])
return ret
if __name__ == '__main__':
img = im.open('wire.png')
print img.mode
img = list(img.getdata())
fim = init()
# print fim
print len(img)
turn = 0
pixDone = 0
secondIterFlag = 0
ctr = 100
tRow = 0
rCol = 99
lCol = 0
bRow = 99
fim[tRow] = split(img[pixDone:pixDone+ctr])
pixDone += ctr
turn += 1
ctr -= 1
tRow += 1
while pixDone <= 10000 and ctr > 0:
# print pixDone
# print ctr
if turn == 0: #top
j = 0
for i in range(lCol,rCol+1):
fim[tRow][i].append(img[pixDone:pixDone+ctr][j])
j+=1
tRow += 1
elif turn == 1: #right
j = 0
for i in range(tRow,bRow+1):
fim[i][rCol].append(img[pixDone:pixDone+ctr][j])
j+=1
rCol -= 1
elif turn == 2: #bottom
j = 0
for i in reversed(range(lCol,rCol+1)):
fim[bRow][i].append(img[pixDone:pixDone+ctr][j])
j+=1
bRow -= 1
elif turn == 3: #left
j = 0
for i in reversed(range(tRow,bRow+1)):
fim[i][lCol].append(img[pixDone:pixDone+ctr][j])
j+=1
lCol += 1
pixDone += ctr
turn += 1
turn %= 4
if secondIterFlag == 0:
secondIterFlag = 1
elif secondIterFlag == 1:
ctr -= 1
secondIterFlag = 0
complete_img = []
for row in fim:
for pix in row:
complete_img.append(pix[0])
# print fim
print len(complete_img)
fim = im.new('RGB',(100,100))
fim.putdata(complete_img)
fim.show()