Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声

  1. User

    用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声

  2. Assistant

    以下是使用Python实现可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声的示例代码:

    import noise
    import numpy as np
    from PIL import Image
    
    
    def generate_perlin_noise_2d(shape, res, octaves, persistence, lacunarity, seed):
        noise_arr = np.zeros(shape)
        for i in range(shape[0]):
            for j in range(shape[1]):
                noise_arr[i][j] = noise.pnoise2(
                    i / res,
                    j / res,
                    octaves=octaves,
                    persistence=persistence,
                    lacunarity=lacunarity,
                    repeatx=shape[0],
                    repeaty=shape[1],
                    base=seed)
        return noise_arr
    
    
    shape = (1024, 1024)
    res = 50.0
    octaves = 6
    persistence = 0.5
    lacunarity = 2.0
    seed = 1
    freq = 3
    
    perlin_noise = generate_perlin_noise_2d(
        shape, res, octaves, persistence, lacunarity, seed)
    
    marble = np.zeros(shape)
    
    # Add color to the image using Pillow module
    for i in range(shape[0]):
        for j in range(shape[1]):
            # Set RGB values
            marble[i][j] = perlin_noise[i][j] * freq
    
    im = Image.fromarray((marble * 255).astype(np.uint8))
    im.show()
    

    在此示例中,我们使用Python第三方库“noise”来生成2D柏林噪声。该库提供了一个名为“pnoise2”的函数,其接受x和y输入并返回2D柏林噪声值。我们使用这个函数,使用嵌套循环为整个图像生成噪声。接下来,我们使用Pillow模块将原始噪声映射到颜色,并将生成的图像显示出来。通过更改传递给“generate_perlin_noise_2d”函数的参数,可以改变噪声的频率、层级数、持续性、波动性和种子值。

  3. User

    用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声

  4. Assistant

    以下是使用Python实现可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声的示例代码:

    import noise
    import numpy as np
    from PIL import Image
    
    
    def generate_perlin_noise_2d(shape, res, octaves, persistence, lacunarity, seed):
        noise_arr = np.zeros(shape)
        for i in range(shape[0]):
            for j in range(shape[1]):
                noise_arr[i][j] = noise.pnoise2(
                    i / res,
                    j / res,
                    octaves=octaves,
                    persistence=persistence,
                    lacunarity=lacunarity,
                    repeatx=shape[0],
                    repeaty=shape[1],
                    base=seed)
        return noise_arr
    
    
    shape = (1024, 1024)
    res = 50.0
    octaves = 6
    persistence = 0.5
    lacunarity = 2.0
    seed = 1
    freq = 3
    
    perlin_noise = generate_perlin_noise_2d(
        shape, res, octaves, persistence, lacunarity, seed)
    
    marble = np.zeros(shape)
    
    # Add color to the image using Pillow module
    for i in range(shape[0]):
        for j in range(shape[1]):
            # Set RGB values
            marble[i][j] = perlin_noise[i][j] * freq
    
    im = Image.fromarray((marble * 255).astype(np.uint8))
    im.show()
    

    在此示例中,我们使用Python第三方库“noise”来生成2D柏林噪声。该库提供了一个名为“pnoise2”的函数,其接受x和y输入并返回2D柏林噪声值。我们使用这个函数,使用嵌套循环为整个图像生成噪声。接下来,我们使用Pillow模块将原始噪声映射到颜色,并将生成的图像显示出来。通过更改传递给“generate_perlin_noise_2d”函数的参数,可以改变噪声的频率、层级数、持续性、波动性和种子值。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.