Perlin噪音看起来条纹不连贯

时间:2017-08-02 20:14:35

标签: python python-3.x python-imaging-library pillow perlin-noise

现在,我的perlin发电机正在“工作”,我创造了噪音,发现它与我在互联网上看到的完全不同...... 我的声音: enter image description here

注意条纹: enter image description here

我的目标是什么(显然有相应的颜色): enter image description here

1: 为什么我的看起来如此吵闹和讨厌?

代码(抱歉没有存根,Perlin噪音构成了程序的大部分,因此包含完整程序非常重要):

    from PIL import Image
from tkinter import filedialog
from random import randint, random

#Initialise width / height
width = 625
height = 625

#Import gradient picture - 200*1 image used to texture perlin noise
#R,G,B,Alpha
gradient = Image.open("image.png")
gradlist = list(gradient.getdata())

#Create new image
img = Image.new('RGBA', (width, height), color=(255, 255, 255, 255))

#Perlin noise modules --------------------------------------------------------------------------------------------------------

#Modules
from random import sample
from math import floor

p = sample([x for x in range(0, (width * height))], (width * height)) * 2

#Antialising
def fade(t):
    retval = 6*(t**5) - 15*(t**4) + 10*(t**3)
    return retval

#Linear interpolation
def lerp(t,a,b):
    retval = a + (t * (b - a))
    return retval

#Clever bitwise hash stuff - picks a unit vector from 12 possible - (1,1,0),(-1,1,0),(1,-1,0),(-1,-1,0),(1,0,1),(-1,0,1),(1,0,-1),(-1,0,-1),(0,1,1),(0,-1,1),(0,1,-1),(0,-1,-1)
def grad(hash, x, y, z):
    h = hash % 15
    if h < 8:
        u = x
    else:
        u = y
    if h < 4:
        v = y
    elif h in (12, 14):
        v = x
    else:
        v = z
    return (u if (h & 1) == 0 else -u) + (v if (h & 2) == 0 else -v)

#Perlin function
def perlin(x,y,z):
    ix = int(floor(x)) & 255
    iy = int(floor(y)) & 255
    iz = int(floor(z)) & 255
    x -= int(floor(x))
    y -= int(floor(y))
    z -= int(floor(z))
    u = fade(x)
    v = fade(y)
    w = fade(z)
    #Complicated hash stuff
    A = p[ix] + iy
    AA = p[A] + iz
    AB = p[A + 1] + iz
    B = p[ix + 1] + iy
    BA = p[B] + iz
    BB = p[B + 1] + iz
    return -lerp(w, lerp(v, lerp(u, grad(p[AA], x, y, z),grad(p[BA], x - 1, y, z)),lerp(u, grad(p[AB], x, y - 1, z),grad(p[BB], x - 1, y - 1, z))),lerp(v, lerp(u, grad(p[AA + 1], x, y, z - 1),grad(p[BA + 1], x - 1, y, z - 1)), lerp(u, grad(p[AB + 1], x, y - 1, z - 1),grad(p[BB + 1], x - 1, y - 1, z - 1))))


def octavePerlin(x,y,z,octaves,persistence):
    total = 0
    frequency = 1
    amplitude = 1
    maxValue = 0
    for x in range(octaves):
        total += perlin(x * frequency, y * frequency, z * frequency) * amplitude
        maxValue += amplitude
        amplitude *= persistence
        frequency *= 2
    return total / maxValue

z = random()
img.putdata([gradlist[int(octavePerlin((x + random() - 0.5) / 1000, (y + random() - 0.5) / 1000, z, 4, 2) * 100 + 100)] for x in range(width) for y in range(height)])
img.save(filedialog.asksaveasfilename() + ".png", "PNG")

0 个答案:

没有答案