Python Challenge[14]

作者: Recgat | 来源:发表于2017-02-11 16:37 被阅读0次

    [Level 14]



    Title: walk around

    这次有两张图片,第二张实际上是100001像素的图片。源码中有提示:remember: 100100 = (100+99+99+98) + (...。实在想不出来,搜索了知道是将第二张图片照第一张图螺旋地重新排列,提示信息括号中指的是从最外圈开始排列的每一圈步数。

    from PIL import Image
    img = Image.open('wire.png')
    newImg = Image.new(img.mode,(100,100))
    y, t, count =0, 0, 0
    while t<10000:
      for x in range(0+count,100-count):
        newImg.putpixel((x,y),img.getpixel((t,0)))
        t = t + 1
      for y in range(0+count,99-count):
        newImg.putpixel((x,y),img.getpixel((t,0)))
        t = t + 1
      for x in range(99-count,0+count,-1):
        newImg.putpixel((x,y),img.getpixel((t,0)))
        t = t + 1
      for y in range(98-count,0+count,-1):
        newImg.putpixel((x,y),img.getpixel((t,0)))
        t = t + 1
      count=1+count
    newImg.show()
    

    得到一幅的图片,修改为cat.html,显示and its name is uzi. you'll hear from him later.,再次修改url,[Level 15]

    小结

    错误的排列可能会得到bitbit.html提示you took the wrong curve.。使用getdata()putdata()方法也可以达到同样效果。

    Python Challenge Wiki

    可以定义方向(或步骤),触发某个条件执行类似转向的操作,或者旋转,然后继续排列。

    from PIL import Image
    src = Image.open('wire.png')
    dst = Image.new(src.mode, (100, 100))
    x, y, idx = -1, 0, 0            # back a step
    steps = [1,0, 0,1, -1,0, 0,-1]
    while idx < 10000:
      nx, ny = x + steps[0], y + steps[1]
      if 0 <= nx < 100 and 0 <= ny < 100 \
      and dst.getpixel((nx, ny)) == (0, 0, 0):
        x, y = nx, ny
        dst.putpixel((x, y), src.getpixel((idx, 0)))
        idx += 1
      else:
        steps = steps[2:] + steps[:2] # turn
    dst.show()
    

    More

    相关文章

      网友评论

        本文标题:Python Challenge[14]

        本文链接:https://www.haomeiwen.com/subject/gmowittx.html