先将这个代码复制,粘帖为文本

_ = (
255,
lambda
V ,B,c
:c and Y(V*V+B,B, c
-1)if(abs(V)<6)else
( 2+c-4*abs(V)**-0.4)/i
) ;v, x=1500,1000;C=range(v*x
);import struct;P=struct.pack;M,\
j ='<QIIHHHH',open('M.bmp','wb').write
for X in j('BM'+P(M,v*x*3+26,26,12,v,x,1,24))or C:
i ,Y=_;j(P('BBB',*(lambda T:(T*80+T**9
*i-950*T **99,T*70-880*T**18+701*
T **9 ,T*i**(1-T**45*2)))(sum(
[ Y(0,(A%3/3.+X%v+(X/v+
A/3/3.-x/2)/1j)*2.5
/x -2.7,i)**2 for \
A in C
[:9]])
/9)
) )
如图片所示
然后改名字,文件后缀为 M.bmp
然后在终端执行$python M.bmp
不要关闭终端,他会根据你的机器配置在你的主文件夹里生成m.bmp(1500x1000)图片。我只要了1分钟 
如图所示

代码我不太懂,无法解释所以然,只是看着好玩,介绍给大家。



+++++++++++++++++++++++++++++
加入我们 /
来 Wow!Ubuntu 问答社区参与更多讨论
20 Responses to 神奇的代码:用 Python 生成分形图片
Alan haha
November 19th, 2012 at 9:45 am
被污染的代码
cc
November 19th, 2012 at 10:54 am
这是什么代码……乱七八糟的。
imdiot
November 19th, 2012 at 10:54 am
亲 复制时注意html转义字符啊……
imdiot
November 19th, 2012 at 11:58 am
! /usr/bin/env python2
-- coding: utf-8 -
import struct
def mandelbrot(V, B, c):
if (abs(V) < 6):
return c and mandelbrot(VV+B, B, c-1)
else:
return (2+c-4abs(V)**-0.4)/255
def pixel_data_handler(T):
red = T * 80 + T ** 9 * 255 - 950 * T ** 99
green = T * 70 - 880 * T ** 18 + 701 * T ** 9
blue = T * 255 ** (1 - T ** 45 * 2)
return (red, green, blue)
width,height=1500,1000;
matrix = range(width*height);
file_write = open('M.bmp','wb').write
init_bmp_data = 'BM'+struct.pack('<QIIHHHH', widthheight3+26, 26, 12, width, height, 1, 24)
file_init_success = file_write(init_bmp_data)
for X in file_init_success or matrix:
mandelbrot_data = sum([mandelbrot(0,(index % 3 / 3. + X % width + (X / width + index / 3 / 3. - height / 2 ) / 1j ) * 2.5 / height - 2.7,255)**2 for index in matrix[:9]])/9
pixel_data = pixel_data_handler(mandelbrot_data)
binary_data = struct.pack('BBB',*pixel_data)
file_write(binary_data)
整理了整理……有请分形大神给大家讲解吧……
imdiot Reply:
November 19th, 2012 at 12:02 pm
@imdiot,
! /usr/bin/env python2
-- coding: utf-8 -
``
import struct
def mandelbrot(V, B, c):
if (abs(V) < 6):
return c and mandelbrot(VV+B, B, c-1)
else:
return (2+c-4abs(V)**-0.4)/255
def pixel_data_handler(T):
red = T * 80 + T ** 9 * 255 - 950 * T ** 99
green = T * 70 - 880 * T ** 18 + 701 * T ** 9
blue = T * 255 ** (1 - T ** 45 * 2)
return (red, green, blue)
width,height=1500,1000;
matrix = range(width*height);
file_write = open('M.bmp','wb').write
init_bmp_data = 'BM'+struct.pack('<QIIHHHH', widthheight3+26, 26, 12, width, height, 1, 24)
file_init_success = file_write(init_bmp_data)
for X in file_init_success or matrix:
mandelbrot_data = sum([mandelbrot(0,(index % 3 / 3. + X % width + (X / width + index / 3 / 3. - height / 2 ) / 1j ) * 2.5 / height - 2.7,255)**2 for index in matrix[:9]])/9
pixel_data = pixel_data_handler(mandelbrot_data)
binary_data = struct.pack('BBB',*pixel_data)
file_write(binary_data)
``
唔……代码怎么高亮……
blambin
November 19th, 2012 at 12:53 pm
看不懂。。
wo0d.z
November 19th, 2012 at 2:48 pm
最为外行,只表示图片很漂亮!
riku
November 19th, 2012 at 4:56 pm
真的画出来了,用了 7 分多钟,看来我的机器确实太烂了。 http://screencloud.net/v/zvsV
riku
November 19th, 2012 at 4:57 pm
yukirock
November 19th, 2012 at 5:19 pm
Mandelbrot Set 么……
zz
November 19th, 2012 at 8:45 pm
这里有个分形程序,把下面的jpg图片下载下来之后,将后缀名改为".rar",然后解压出一个jar文件,这是个java程序,装了jre就能运行。可以在分形图片上选择一个小区域双击放大。
http://dl.iteye.com/upload/picture/pic/120242/3d82b1f7-e1d9-3746-b63c-267cdad42036.jpg
唯美落叶
November 19th, 2012 at 10:43 pm
查了一下,运行时只占用了一个线程,所以很慢……怎么能在编译时用多个线程啊?我的cpu是双核四线程的,-j4参数不好用……
ssss
November 20th, 2012 at 2:42 pm
nice
第三眼
November 22nd, 2012 at 4:31 pm
代码写成一条鱼就是为了摆个形吧...,不过这缩进得好好研究了
asjiko
November 24th, 2012 at 1:50 pm
looks cool...
luc
November 29th, 2012 at 4:19 am
Hao lihai de yangzi.
yakon
December 3rd, 2012 at 8:38 pm
这,难道是混沌代码?
上次看混沌理论时,就是这种图片。这能不能无限循环,每台机器的图会不会不一样。
yakon
December 3rd, 2012 at 8:38 pm
这,难道是混沌代码?
上次看混沌理论时,就是这种图片。这能不能无限循环,每台机器的图会不会不一样。
ae868186
December 9th, 2012 at 11:10 pm
我知道这个图片,无穷放大的效果,
sysop
April 10th, 2013 at 3:05 pm
太神奇了 呵呵