gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- User
用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声
- Assistant
以下是一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声的示例代码:
import numpy as np from PIL import Image def noise_2d(width, height, frequency, lacunarity, persistence, octaves, seed): """ 2D Perlin noise generator. Args: width (int): Width of the output image. height (int): Height of the output image. frequency (float): Base frequency of the noise. lacunarity (float): Factor by which the frequency changes with each octave. persistence (float): Factor by which the amplitude changes with each octave. octaves (int): Number of octaves to use. seed (int): Seed for the random number generator. Returns: A 2D NumPy array of the generated noise. """ np.random.seed(seed) # Generate a grid of random gradient vectors. gradients = np.random.normal(size=(width + 1, height + 1, 2)) gradients /= np.sqrt(np.sum(gradients ** 2, axis=2, keepdims=True)) x, y = np.meshgrid(np.arange(width), np.arange(height)) x = x / frequency y = y / frequency # Initialize the noise array with zeros. noise = np.zeros((width, height)) amplitude = 1.0 for octave in range(octaves): # Calculate the coordinates of the corner points of the current cell. x0 = np.floor(x).astype(int) y0 = np.floor(y).astype(int) x1 = x0 + 1 y1 = y0 + 1 # Calculate the relative position of the coordinates within the cell. dx = x - x0 dy = y - y0 # Calculate the dot products of the gradient vectors and the vectors from the # corner points to the input coordinates. dot00 = (gradients[x0, y0, 0] * dx + gradients[x0, y0, 1] * dy) dot10 = (gradients[x1, y0, 0] * (dx - 1) + gradients[x1, y0, 1] * dy) dot01 = (gradients[x0, y1, 0] * dx + gradients[x0, y1, 1] * (dy - 1)) dot11 = (gradients[x1, y1, 0] * (dx - 1) + gradients[x1, y1, 1] * (dy - 1)) # Interpolate the dot products using smoothstep functions. sx = smoothstep(dx) sy = smoothstep(dy) # Interpolate the dot products to get the final noise value for this octave. noise += amplitude * lerp(lerp(dot00, dot10, sx), lerp(dot01, dot11, sx), sy) # Scale the frequency and amplitude for the next octave. frequency *= lacunarity amplitude *= persistence # Normalize the noise array to the range [0, 1]. noise = (noise - np.min(noise)) / (np.max(noise) - np.min(noise)) return noise def smoothstep(t): """ Smoothstep function. Args: t (float or numpy.ndarray): Input value(s) between 0 and 1. Returns: The smoothed output value(s). """ return 6 * t**5 - 15 * t**4 + 10 * t**3 def lerp(a, b, t): """ Linear interpolation function. Args: a (float or numpy.ndarray): Start value(s). b (float or numpy.ndarray): End value(s). t (float or numpy.ndarray): Interpolation factor(s) between 0 and 1. Returns: The interpolated value(s) between a and b. """ return a + t * (b - a) # Generate a 512x512 Perlin noise image with the given parameters. width = height = 512 frequency = 0.01 lacunarity = 2.0 persistence = 0.5 octaves = 8 seed = 12345 noise = noise_2d(width, height, frequency, lacunarity, persistence, octaves, seed) # Save the image as a PNG file. img = Image.fromarray((noise * 255).astype(np.uint8), mode='L') img.save('perlin.png')这个代码会生成一个512x512的灰度图像,可以使用以下方式调用:
width = height = 512 frequency = 0.01 lacunarity = 2.0 persistence = 0.5 octaves = 8 seed = 12345 noise = noise_2d(width, height, frequency, lacunarity, persistence, octaves, seed)您可以将上述参数更改为您想要的值。生成的噪声数组的范围在0到1之间。如果需要,可以乘以一个系数来放大噪声的振幅。将噪声数组转换为图像并保存为PNG文件可以使用以下代码:
img = Image.fromarray((noise * 255).astype(np.uint8), mode='L') img.save('perlin.png')这将保存名为“ perlin.png”的文件,其中包含生成的噪声图像。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →
- gpt-3.5-turbo-0301Compare side by side →