|
1 | 1 | import requests
|
2 | 2 | import wave
|
| 3 | +from PIL import Image |
| 4 | +from PIL import ImageDraw |
3 | 5 |
|
4 | 6 |
|
5 | 7 | def main():
|
6 | 8 | seed = "http://www.pythonchallenge.com/pc/hex/"
|
7 | 9 | file_path = "lake/"
|
8 | 10 | auth = ("butter", "fly")
|
9 | 11 | # first_step(seed, file_path, auth)
|
10 |
| - bs = second_step() |
11 |
| - third_step(bs) |
| 12 | + # bss = second_step() |
| 13 | + # all_pixel = third_step(bss) |
| 14 | + # fourth_step(all_pixel) |
| 15 | + fifth_step() |
12 | 16 |
|
13 | 17 |
|
14 | 18 | def first_step(seed, file_path, auth):
|
@@ -37,21 +41,73 @@ def second_step():
|
37 | 41 | return bsAry
|
38 | 42 |
|
39 | 43 |
|
40 |
| -def third_step(bs): |
| 44 | +def third_step2(bss): |
41 | 45 | result_file = wave.open("lake/result.wav", "wb")
|
42 | 46 | src_file = wave.open("lake/1.wav", "rb")
|
43 | 47 | nframes = src_file.getnframes()
|
44 | 48 | result_file.setnchannels(src_file.getnchannels())
|
45 | 49 | result_file.setsampwidth(src_file.getsampwidth())
|
46 | 50 | result_file.setframerate(src_file.getframerate())
|
47 | 51 | result_file.setnframes(nframes * 25)
|
48 |
| - for i in range(len(bs)): |
49 |
| - b = bs[i] |
50 |
| - result_file.writeframesraw(b) |
| 52 | + for i in range(len(bss)): |
| 53 | + bs = bss[i] |
| 54 | + result_file.writeframesraw(bs) |
51 | 55 | src_file.close()
|
52 | 56 | result_file.close()
|
53 | 57 | print("Done")
|
54 | 58 |
|
55 | 59 |
|
| 60 | +def third_step(bss): |
| 61 | + all_pixel = [] |
| 62 | + for i in range(len(bss)): |
| 63 | + bs = bss[i] |
| 64 | + bs_len = len(bs) |
| 65 | + j = 0 |
| 66 | + pixels = [] |
| 67 | + while True: |
| 68 | + if j >= bs_len: |
| 69 | + break; |
| 70 | + x = bs[j] |
| 71 | + j += 1 |
| 72 | + if j >= bs_len: |
| 73 | + break; |
| 74 | + y = bs[j] |
| 75 | + j += 1 |
| 76 | + if j >= bs_len: |
| 77 | + break; |
| 78 | + z = bs[j] |
| 79 | + j += 1 |
| 80 | + pixel = (x, y, z) |
| 81 | + pixels.append(pixel) |
| 82 | + all_pixel.append(pixels) |
| 83 | + print(all_pixel) |
| 84 | + return all_pixel |
| 85 | + |
| 86 | + |
| 87 | +def fourth_step(all_pixel): |
| 88 | + for i in range(len(all_pixel)): |
| 89 | + im = Image.new("RGB", (60, 60)) |
| 90 | + drawer = ImageDraw.Draw(im) |
| 91 | + pixels = all_pixel[i] |
| 92 | + count = 0 |
| 93 | + for x in range(60): |
| 94 | + for y in range(60): |
| 95 | + print("drawing i={}, count={}, x={}, y={}".format(i, count, x, y)) |
| 96 | + drawer.point((x, y), pixels[count]) |
| 97 | + count += 1 |
| 98 | + im.save("lake/pic/{}.png".format(i)) |
| 99 | + |
| 100 | + |
| 101 | +def fifth_step(): |
| 102 | + im = Image.new("RGB", (300, 300)) |
| 103 | + for i in range(25): |
| 104 | + x = (i // 5) * 60 |
| 105 | + y = (i % 5) * 60 |
| 106 | + im_tmp = Image.open("lake/pic/{}.png".format(i)) |
| 107 | + im.paste(im_tmp, (x, y)) |
| 108 | + print("i={}, x={}, y={}".format(i, x, y)) |
| 109 | + im.save("lake/pic/result.png") |
| 110 | + |
| 111 | + |
56 | 112 | if __name__ == "__main__":
|
57 | 113 | main()
|
0 commit comments