Python Challenge (level 14)

该博客介绍了一个基于Python的挑战,通过解析一张名为'wire.png'的图片文件并重新排列其像素来形成新的图像。使用数学计算确定每个像素的新位置,最终得到一张100x100像素的新图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

URI: [url]http://www.pythonchallenge.com/pc/return/italy.html[/url]
Username: huge; password: file
说明:

解题答案:
import Image, math

def get_pixel(t):
t = (100 * 100 - 1) - t
shell = int((math.sqrt(t) + 1) / 2)
leg = 0 if (shell == 0) else int((t - (2 * shell - 1) ** 2) / 2 / shell)
elt = t - (2 * shell - 1) ** 2 - 2 * shell * leg - shell + 1

if leg == 0:
x, y = shell, elt
elif leg == 1:
x, y = -elt, shell
elif leg == 2:
x, y = -shell, -elt
else:
x, y = elt, -shell

return (49 + x, 50 - y)

# Download the image from: http://www.pythonchallenge.com/pc/return/wire.png

input = Image.open("wire.png")
output = Image.new("RGB", (100, 100))

for i, px in enumerate(input.getdata()):
output.putpixel(get_pixel(i), px)

output.save("new.jpg")

过关答案:
cat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值