Python柏林噪声

参考资料:
1、WilenWu-CSDN博客_python 柏林噪声:https://blog.csdn.net/qq_41518277/article/details/82779516
2、童晶《Python趣味创意编程》第10章
3、http://libnoise.sourceforge.net/index.html
4、https://www.cnblogs.com/leoin2012/p/7218033.html
5、Sengo的CSDN博客:https://blog.csdn.net/Sengo_GWU/article/details/80153638
6、《不只是噪声,更是数学美》:https://blog.csdn.net/qq_34302921/article/details/80849139

一、柏林噪声

   WiKi解释:Perlin Noise是Ken Perlin在1983年开发的一种梯度噪音,这是一种用于在计算机生成的表面上产生自然出现纹理的技术,使用Perlin噪声合成的纹理通常用于CGI,通过模仿自然界中纹理的受控随机外观,使计算机生成的视觉元素(如物体表面,火焰,烟雾或云)看起来更自然。2D柏林噪声可以通过插值生成地形,而3D柏林噪声则可以模拟海平面上起伏的波浪。

二、noise 库的下载与安装

下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#noise

安装

pip install noise-1.2.3-cp38-cp38-win_amd64.whl

三、例程

1、随机画线

把生成的随机点存到列表里,就可以随机画线了

  • snoise2() 返回的是一个 (-1,1)之间的数,所以要乘以长宽比例进行放大
  • 用 W/2 和 H/2 减,是实现坐标变换,以屏幕中心为原点
  • 现在把 snoise2(x,y) 当一维噪声来调用,所以有一个参数要设置为常量
  • scale 越大,随机数前后变化幅度越小,表现就越丝滑
from PIL import Image, ImageDraw
import random
from noise import snoise2
global px,py

def gen_point():
    global px,py,points
    for i in range(20):
        px += 1
        py += 1
        x = int(W/2 - snoise2(px/scale,seed_y)*x_scale)
        y = int(H/2 - snoise2(seed_x,py/scale)*y_scale)
        points.append((x,y))
        if len(points) >10 and  (x,y) == points[0]:
            lines.append(points)
            points=[]
        if len(points)>1000:
            points.pop(0)

# 创建一个白色画布 
# RGB mode and size 800x600 ,
W = 800
H = 600
x_scale,y_scale,scale = W/2,H/2,512
seed_x,seed_y = random.random(),random.random()

points = []
px,py = random.randint(0,W),random.randint(0,H)

image = Image.new('RGBA', (W, H), (255,255,255))
d = ImageDraw.Draw(image)

for i in range(100):
    d.line(points,(255,0,0),2,0) #红色渐变曲线
    gen_point()
    i=i+1
    
image.show()

2、运行结果

柏林噪声生成的曲线
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容