关卡地址

解决方案:

思路:

图片每一行都有一条7个像素组成的像素块,两端是1个白色像素,中间是5个粉紫色像素,色值是(255,0,255)。由于图像模式是P,使用8位映射到调色板。

经测试,连续5个粉紫色像素的值是195。

根据标题:

让我直说吧

将这些像素块排成一列,就会得到下一关地址啦。

代码:

Challenge016.py

import helper
path=".\\Data\\016"
helper.ensureDir(path)

# ================================

# 使用http认证,下载文件

import urllib.request
helper.installHTTPBasicAuthOpener("huge", "file")

gif="http://www.pythonchallenge.com/pc/return/mozart.gif"
(filename, headers)=urllib.request.urlretrieve(gif, path+"\\mozart.gif")
# ================================


from PIL import Image
im=Image.open(filename)
px=im.load()

newIm=Image.new(im.mode, im.size)
newPx=newIm.load()

(w,h)=im.size
for y in range(h):
    for x in range(w):
        if px[x,y] == 195:
            for xx in range(w):
                newPx[xx,y]=px[(xx+x)%w,y]
            continue

im.close()
newIm.show()
newIm.close()
PS src\static> python .\Code\Python\Challenge016.py

Challenge016.ps1

$path=".\\Data\\016"

. .\Code\PowerShell\helper.ps1
New-Dir -Dir $path

$gif="http://www.pythonchallenge.com/pc/return/mozart.gif"
$filename=$path+"\\mozart.gif"
Get-FileWithAuth -Url $gif -Filename $filename -Username "huge" -Password "file"

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$img=[System.Drawing.Image]::FromFile($filename)
$newImg=New-Object System.Drawing.Bitmap $img.Width,$img.Height
$color=[System.Drawing.Color]::FromArgb(255,0,255)

for ($y = 0; $y -lt $img.Height; $y++) {
    for ($x = 0; $x -lt $img.Width; $x++) {
        if ($img.GetPixel($x,$y) -eq $color) {
            for ($xx = 0; $xx -lt $img.Width; $xx++) {
                $newImg.SetPixel($xx,$y,$img.GetPixel(($xx+$x)%$img.Width,$y))
            }
            continue
        }
    }
}

$img.Dispose()

Show-Image -Title "Challenge016" -Image $newImg
$newImg.Dispose()
PS src\static> .\Code\PowerShell\Challenge016.ps1

Challenge016.go

package main

import(
	"image"
)

func (c *Challenge) Challenge016() {
	path:=".\\Data\\016"
	EnsureDir(path)

	gif:="http://www.pythonchallenge.com/pc/return/mozart.gif"
	filename:=path+"\\mozart.gif"
	DownloadWithBasicAuth(gif, filename, "huge", "file")

	im:=OpenImage(filename)

	newImg:=straightImage(im)

	giffile := path+"\\mozart.go.gif"
	SaveImage(giffile, newImg, "gif")
	
	ShowImage(giffile)
}

func straightImage(im image.Image) *image.RGBA {
	bounds:=im.Bounds()
	newImg:=image.NewRGBA(bounds)
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r,g,b,_:=im.At(x,y).RGBA()
			if r==65535 && g==0 && b==65535 {
				for xx := bounds.Min.X; xx < bounds.Max.X; xx++ {
					newImg.Set(xx,y,im.At((xx+x)%bounds.Dx(),y))
				}
				continue
			}
		}
	}
	return newImg
}
PS src\static> .\Code\Go\Challenge.exe -l 016

最终结果: romance

[下一关地址][5]