Skip to content

Commit 045c079

Browse files
22
1 parent b162c43 commit 045c079

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

22.py

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
1-
from PIL import Image, ImageDraw
1+
from PIL import Image
2+
from PIL import ImageSequence
3+
import turtle, time
24

35

46
def main():
5-
im, width, height = first_step()
6-
second_step(im, width, height)
7-
8-
9-
def first_step():
10-
im = Image.open("copper/white.gif")
11-
size = im.size
12-
width = size[0]
13-
height = size[1]
14-
return im, width, height
15-
16-
17-
def second_step(im, width, height):
18-
pass
7+
file_path = "copper/white.gif"
8+
pixels = first_step(file_path)
9+
"""
10+
pixels = [(100, 100), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102),
11+
(100, 102), (102, 102), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100),
12+
(102, 100), (102, 98), (100, 98), (100, 98), (100, 98), (98, 98), (98, 100), (98, 100), (98, 100),
13+
(98, 100), (98, 100), (98, 100), (98, 102), (100, 100), (98, 98), (98, 98), (98, 100), (98, 100),
14+
(98, 100), (98, 100), (98, 100), (98, 102), (98, 102), (98, 102), (100, 102), (100, 102), (102, 102),
15+
(102, 102), (102, 102), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 98), (102, 98),
16+
(102, 98), (100, 98), (100, 98), (100, 100), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102),
17+
(100, 102), (100, 102), (102, 100), (100, 98), (100, 98), (100, 98), (102, 98), (102, 98), (102, 98),
18+
(102, 98), (102, 100), (102, 100), (102, 102), (102, 102), (102, 102), (100, 102), (100, 102), (100, 102),
19+
(100, 102), (100, 100), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102), (100, 102),
20+
(102, 102), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 98), (100, 98),
21+
(100, 98), (100, 98), (100, 98), (100, 98), (100, 98), (100, 100), (98, 98), (98, 100), (98, 100),
22+
(98, 100), (98, 100), (98, 100), (98, 100), (98, 100), (98, 102), (100, 102), (100, 102), (102, 102),
23+
(102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 100), (102, 102),
24+
(100, 102), (100, 102), (98, 102), (98, 100), (98, 100), (98, 100), (98, 100), (98, 100), (98, 100),
25+
(98, 100), (98, 98)]
26+
"""
27+
second_step(pixels)
28+
29+
30+
def first_step(file_path):
31+
im = Image.open(file_path)
32+
pixels = []
33+
for frame in ImageSequence.Iterator(im):
34+
size = frame.size
35+
width = size[0]
36+
height = size[1]
37+
for x in range(width):
38+
for y in range(height):
39+
pixel = frame.getpixel((x, y))
40+
if pixel != 0:
41+
pixels.append((x, y))
42+
print(pixels)
43+
return pixels
44+
45+
46+
def second_step(pixels):
47+
EXP = 10
48+
x = y = 0
49+
for pilx, pily in pixels:
50+
dx = (pilx - 100) // 2
51+
dy = (100 - pily) // 2
52+
x += dx * EXP
53+
y += dy * EXP
54+
turtle.goto(x, y)
55+
if dx == dy == 0:
56+
time.sleep(1)
57+
turtle.reset()
58+
x = y = 0
1959

2060

2161
if __name__ == "__main__":

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# pythonchallenge
22
Solve Problems in http://www.pythonchallenge.com
33

4+
#### 22
5+
result: http://www.pythonchallenge.com/pc/hex/bonus.html
6+
```python
7+
from PIL import Image
8+
from PIL import ImageSequence
9+
import turtle, time
10+
```
11+
- use ImageSequence to get the frame number of a gif
12+
13+
```python
14+
a = 10
15+
b = 3
16+
print(a / b) # 3.3333333333333335
17+
print(a // b) # 2
18+
print(a % b) # 1
19+
```
20+
21+
422
#### 21
523
result: http://www.pythonchallenge.com/pc/hex/copper.html
624
```python

0 commit comments

Comments
 (0)