python挑战之level 24

来源:互联网 发布:国外数据采集 的方法 编辑:程序博客网 时间:2024/06/07 10:44

连接:http://www.pythonchallenge.com/pc/hex/ambiguity.html

登陆密码;(butter,fly)

推荐视频:http://v.youku.com/v_show/id_XMTQ3NjUyMzMzMg==.html?&f=26483447&from=y1.2-3.4.25&spm=a2h0j.8191423.item_XMTQ3NjUyMzMzMg==.A

————————————————————————————————————————————————————————————

这是一幅黄色的像素图

标题:from top to bottom

注释:无

从顶部到底部,要我们干啥?图片名字:maze.png

maze英[meɪz]美[mez]n.迷宫; 迷惑; 错综复杂; 迷宫图;vt.使困惑; 使混乱; 迷失;[例句]The palace has extensive gardens, a maze, and tennis courts.这座宫殿有几座大花园、一处迷宫和几个网球场。[其他]第三人称单数:mazes 复数:mazes 现在分词:mazing 过去式:mazed 过去分词:mazed

难道让我们从中解出一个迷宫来?如何来做呢?放大图片。我们可以看见图片右上角是迷宫入口,左下角是出口,白色是墙,其中路径中充满了黑色和红色的点。按照前面几关的经验。我们需要把红色取出来放到另一个图片。这可能是一个线索

————————————————————————————————————————————————————————————

迷宫算法。我没写出来,炒的是视频上面的,

——————————————————————————————————————————————————————————————

运行程序:

from PIL import  Imagefrom time import timefrom astar import AStarimport remaze_image = Image.open('maze.png')maze_start = (639,0)maze_end = (1,640)time_start = time()maze = AStar(maze_image,maze_start,maze_end)time_end = time()print('1-创建迷宫对象所用时间为: ',time_end-time_start)time_start = time()path = maze.get_path()print('2-找出路径时间为: ',time()-time_start)#在maze_image上将最短路径画出来lv = (0,255,0)#方法重命名,提高可读性hua = maze_image.putpixellujing=path#画图for zuobiao in lujing :    hua(zuobiao,lv)maze_image.save('maze_path.png')#将路径上红色节点的RGB色中R色组成字节码#几种方法print('3-获取红色数据列表: ')#方法1:#红色的点都在奇数索引上time_start = time()red_colors = []for xy in path[1::2]:    red_colors.append(maze_image.getpixel(xy)[0])print('方法1使用时间: ',time()-time_start)#方法2,先取出所有点的颜色,列表索引的方法time_start = time()color_data = maze_image.getdata()red_colors = []for x, y in path[1::2]:    data = color_data[maze.width*y+x][0]    red_colors.append(data)    with open('red.txt','wb')as f:        f.write((chr(data)).encode('utf-8'))print('方法2使用时间: ',time()-time_start)#方法3time_start = time()color_data = maze_image.getdata()w = maze.widthred_colors = [color_data[w*y +x][0] for (x,y) in path[1::2] ]print('方法3使用时间: ',time()-time_start)print(len(red_colors),red_colors[:10])'''#创建字节码在我的电脑中,我运行的是python3,但是我发现在我本机上面,使用chr()函数获取ascii码chr(61)-->'a' ,《class 'str'>我们需要把他转为bytes类型--》chr(3).encode('utf-8')得到的是\x61,这是对的但是:在转超过128的ascii时,chr(248).encode('utf-8')--》\xc3xb8,变成了两个字节,所以使用下面的方法,得到正确的十六进制我不知道为什么。。。
补充一点:不用使用我下面的方法:<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;">一个支持Extended ASCII部分字符,且采用1个Byte固定大小编码的encoding,比如ISO 8859-1,也被称为latin1</span>
<span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;">>>> chr(165).encode('latin1')</span><br style="margin: 0px; padding: 0px; font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;" /><span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;">b'\xa5'</span>使用上面latin1编码即可
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;"><span style="margin: 0px; padding: 0px;">参考资料:</span></p><span style="font-family: 'Microsoft YaHei', Verdana, sans-serif, 宋体; font-size: 14px;">* 《Fluent Python》, Chapter 4</span>'''data = ''.join(chr(i) for i in red_colors)num=re.split(' ',data)data_tmp=b''for i in num:    if i.isdigit():        t = int(i)        if t <128:            data_tmp+=chr(t).encode('utf-8')        else:            #例如:hex()得到的是:0x33(这个长度是4个字节),取后面33            tmp=hex(t)[2:]            #获得规范的十六进制,得到一个字节            tmp2b=bytes.fromhex(tmp)            data_tmp+=tmp2boutput=open('maze.zip','w')output.write(data_tmp)output.close()print('4-文件已经保存到zip中')

——————————————————————————————————————————————

结果为:

一张图片和一个压缩包,压缩包是后面用到的

下一关:http://www.pythonchallenge.com/pc/hex/lake.html


0 0
原创粉丝点击