Skip to content

Commit 40f5d73

Browse files
committed
Refactor solution to mission 25
1 parent 67c7de7 commit 40f5d73

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

25-lake.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212

1313
from auth import get_last_src_url
1414
from auth import read_url
15+
from cache import autocached
16+
from image import image_to_text
1517
from itertools import chain
1618
from PIL import Image
1719

1820
import io
1921
import wave
2022

2123

24+
@autocached
2225
def read_waves(url):
2326
"""Reads all the WAVE files there are available under similar URLs"""
2427
i, waves = 1, []
@@ -32,6 +35,7 @@ def read_waves(url):
3235
return waves
3336

3437

38+
@autocached
3539
def create_image(waves):
3640
"""Creates an image out of the WAVE frames"""
3741
wav_size = int((len(waves[0]) / 3) ** 0.5) # a square with len(waves[0]) RGB pixels
@@ -52,7 +56,18 @@ def create_image(waves):
5256
return new_image
5357

5458

59+
@autocached
60+
def blue_only(image):
61+
"""Creates a new image with only the bluest pixels"""
62+
img = Image.new("L", (image.width, image.height))
63+
for y in range(image.height):
64+
for x in range(image.width):
65+
r, g, b = image.getpixel((x, y))
66+
if b > 1.2 * r and b > 1.2 * g:
67+
img.putpixel((x, y), b)
68+
return img
69+
70+
5571
url = get_last_src_url("http://www.pythonchallenge.com/pc/hex/lake.html")
56-
image_name = "25-lake.png"
57-
create_image(read_waves(url.replace("jpg", "wav"))).save(image_name)
58-
print("Check", image_name)
72+
image = create_image(read_waves(url.replace("jpg", "wav")))
73+
print(image_to_text(blue_only(image)))

0 commit comments

Comments
 (0)