继续挑战,开始变得梦幻了
第25题地址lake.html
- 网页标题是
imagine how they sound
,题目内容为空,源码里面有一句隐藏内容:<!– can you see the waves? –>
图片是一张湖的图片,不过分割成了5*5拼图的样子。
注意到图片名字叫lake1.png
,马上想到去找lake2.png
,发现并不存在。
再看到标题说sound
和提示里面有waves
,我们想到可以试试lake1.wav
:
import requests
lakes = []
with requests.Session() as sess:
sess.auth = ('butter', 'fly')
index = 1
while True:
response = sess.get(f'http://www.pythonchallenge.com/pc/hex/lake{index}.wav')
if response.status_code == 404:
break
lakes.append(response.content)
index += 1
print('waves:', len(lakes))
waves: 25
按这个思路拿到了25个音频文件。我们研究一下:
import wave
from io import BytesIO
from IPython.display import Audio
for lake in lakes:
with wave.open(BytesIO(lake), 'r') as f:
print(f.getparams())
Audio(lakes[0], autoplay=True)
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
_wave_params(nchannels=1, sampwidth=1, framerate=9600, nframes=10800, comptype='NONE', compname='not compressed')
音频都是噪声。但全部音频都是相同的采样率和帧数。
看来需要一点想像力了。
总共是25个音频,而题目的图片是一张5*5的拼图,数量是对得上的。
加上标题说imagine how they sound
想象这些声音,
嗯是的,
是要把这些音频按拼图的方式拼起来。
音频的nchannels
频道数和sampwidth
采样宽度都是1,说明每一帧的大小都是一个字节——0~255
,
而nframes
帧数是10800,想象一下每3帧构成一个像素的RGB
值,就会有\(10800\div3=3600\)个像素。
如果这些像素按拼图的方式拼起来,每一块的大小就应该是60*60!!
听上去计划通,我们来实施一下:
import wave
from io import BytesIO
from PIL import Image
single = 60
size = 5
img = Image.new('RGB', (size * single, size * single))
for i, lake in enumerate(lakes):
with wave.open(BytesIO(lake), 'r') as f:
data = f.readframes(f.getnframes())
img_single = Image.frombytes('RGB', (single, single), data)
x, y = i % size, i // size
img.paste(img_single, (x * single, y * single, (x + 1) * single, (y + 1) * single))
img
好了,地址改为decent.html,来到了下一题,还算顺利。