Python噪波库中的柏林噪波

2024-05-19 16:25:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个为我的项目产生柏林噪音的问题。因为我想了解如何正确地使用库,所以我尝试一步一步地遵循这个页面:https://medium.com/@yvanscher/playing-with-perlin-noise-generating-realistic-archipelagos-b59f004d8401 在第一部分中,有代码:

import noise
import numpy as np
from scipy.misc import toimage

shape = (1024,1024)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0

world = np.zeros(shape)
for i in range(shape[0]):
    for j in range(shape[1]):
        world[i][j] = noise.pnoise2(i/scale, 
                                    j/scale, 
                                    octaves=octaves, 
                                    persistence=persistence, 
                                    lacunarity=lacunarity, 
                                    repeatx=1024, 
                                    repeaty=1024, 
                                    base=0)

toimage(world).show()

我复制粘贴它,并在末尾进行小改动(toimage已过时),因此我有:

import noise
import numpy as np
from PIL import Image

shape = (1024,1024)
scale = 100
octaves = 6
persistence = 0.5
lacunarity = 2.0
seed = np.random.randint(0,100)

world = np.zeros(shape)
for i in range(shape[0]):
    for j in range(shape[1]):
        world[i][j] = noise.pnoise2(i/scale,
                                    j/scale,
                                    octaves=octaves,
                                    persistence=persistence,
                                    lacunarity=lacunarity,
                                    repeatx=1024,
                                    repeaty=1024,
                                    base=seed)

Image.fromarray(world, mode='L').show()

我尝试了很多衍射模式,但这种噪声甚至不接近相干噪声。我的结果类似于this(mode='L')。谁能解释一下,我做错了什么


Tags: inimportnumpyforworldasnprange
2条回答

如果有人来找我,你应该用噪声库正常化

img = np.floor((world + 1) * 127).astype(np.uint8)

这样,就不会有任何异常的斑点颜色相反,它应该是什么

这是工作代码。我冒昧地把它清理了一下。有关详细信息,请参见注释。最后一个建议是:在测试代码时,使用matplotlib进行可视化。它的imshow()函数比PIL更健壮

import noise
import numpy as np
from PIL import Image

shape = (1024,1024)
scale = .5
octaves = 6
persistence = 0.5
lacunarity = 2.0
seed = np.random.randint(0,100)

world = np.zeros(shape)

# make coordinate grid on [0,1]^2
x_idx = np.linspace(0, 1, shape[0])
y_idx = np.linspace(0, 1, shape[1])
world_x, world_y = np.meshgrid(x_idx, y_idx)

# apply perlin noise, instead of np.vectorize, consider using itertools.starmap()
world = np.vectorize(noise.pnoise2)(world_x/scale,
                        world_y/scale,
                        octaves=octaves,
                        persistence=persistence,
                        lacunarity=lacunarity,
                        repeatx=1024,
                        repeaty=1024,
                        base=seed)

# here was the error: one needs to normalize the image first. Could be done without copying the array, though
img = np.floor((world + .5) * 255).astype(np.uint8) # <- Normalize world first
Image.fromarray(img, mode='L').show()

相关问题 更多 >