Write Perlin noise height map generator on Python. Input params: width, height, width…
- User
Write Perlin noise height map generator on Python. Input params: width, height, width resolution, height resolution, seed, min value, max value
- Assistant
Creating a Perlin noise height map generator in Python involves using a Perlin noise library, such as
noise, which can be installed via pip. This generator will accept your specified parameters: width, height, width resolution, height resolution, seed, min value, and max value.Here’s a simple implementation for generating a height map using Perlin noise:
Step 1: Install the
noiselibraryFirst, you will need to install the
noiselibrary. You can do this with pip:pip install noiseStep 2: Create the Perlin noise height map generator
Now, let's create a Python script to generate a height map based on the input parameters.
import numpy as np import matplotlib.pyplot as plt from noise import snoise2 def generate_perlin_height_map(width, height, width_res, height_res, seed, min_value, max_value): # Create an empty array for the height map height_map = np.zeros((height, width)) # Loop through each pixel in the height map for y in range(height): for x in range(width): # Generate Perlin noise value for the given coordinates, scaled by the resolution noise_value = snoise2(x / width_res, y / height_res, octaves=6, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=seed) # Map the noise value from its range (-1 to 1) to the desired range (min_value to max_value) height_map[y][x] = (noise_value + 1) / 2 * (max_value - min_value) + min_value return height_map def show_height_map(height_map): plt.imshow(height_map, cmap='terrain') plt.colorbar() plt.title('Perlin Noise Height Map') plt.show() # Example usage if __name__ == "__main__": width = 100 height = 100 width_res = 10.0 height_res = 10.0 seed = 42 min_value = 0.0 max_value = 1.0 height_map = generate_perlin_height_map(width, height, width_res, height_res, seed, min_value, max_value) show_height_map(height_map)Explanation:
-
Parameters:
width: The width of the height map.height: The height of the height map.width_res: Horizontal resolution, controls the 'scale' of the Perlin noise.height_res: Vertical resolution, controls the vertical 'scale'.seed: Base value for generating Perlin noise which allows repeatability.min_valueandmax_value: These are the mapped output values of the noise to define the altitude range.
-
Using
snoise2: Thesnoise2function generates 2D Perlin noise based on the given coordinates. We combine the effects ofwidth_resandheight_reson the input coordinates to determine the frequency and details of the noise.
Visualization:
The generated height map is visualized using
matplotlib. It uses a terrain colormap to give an intuitive representation of the height values.Run the Code:
Simply execute the script to generate and visualize the Perlin noise height map. Adjust the parameters as desired to see different effects in the noise generation.
-
- User
finally, save height map to png file directly from numpy. do not use plt
- Assistant
To save a height map generated from Perlin noise directly to a PNG file without using
matplotlib, you can use thePIL(Pillow) library. Here's how to modify the previous example to include saving the height map to a PNG file:Step 1: Install Pillow
If you don't have Pillow installed, you can do so via pip:
pip install PillowStep 2: Modify the Script to Save the Height Map
Here’s the complete script that generates the Perlin noise height map and saves it as a PNG file:
import numpy as np from noise import snoise2 from PIL import Image def generate_perlin_height_map(width, height, width_res, height_res, seed, min_value, max_value): # Create an empty array for the height map height_map = np.zeros((height, width)) # Loop through each pixel in the height map for y in range(height): for x in range(width): # Generate Perlin noise value for the given coordinates, scaled by the resolution noise_value = snoise2(x / width_res, y / height_res, octaves=6, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=seed) # Map the noise value from its range (-1 to 1) to the desired range (min_value to max_value) height_map[y][x] = (noise_value + 1) / 2 * (max_value - min_value) + min_value return height_map def save_height_map_to_png(height_map, file_name): # Normalize the height map to the range 0-255 for image representation normalized_map = (height_map - np.min(height_map)) / (np.max(height_map) - np.min(height_map)) * 255 # Convert to uint8 type image_data = normalized_map.astype(np.uint8) # Create a PIL Image from the numpy array and save it image = Image.fromarray(image_data, mode='L') # 'L' mode for grayscale image.save(file_name) # Example usage if __name__ == "__main__": width = 100 height = 100 width_res = 10.0 height_res = 10.0 seed = 42 min_value = 0.0 max_value = 1.0 height_map = generate_perlin_height_map(width, height, width_res, height_res, seed, min_value, max_value) # Save height map to PNG file save_height_map_to_png(height_map, 'height_map.png')Explanation:
-
Pillow Usage: The
PIL.Imagemodule from Pillow is used to convert the NumPy array representing the height map into an image. -
Normalization: The height map values are normalized to the range
0-255, which is suitable for grayscale image representation. -
Saving the Image: The
Image.fromarraymethod creates a grayscale image from the NumPy array, andimage.save(file_name)saves it to the specified file.
Notes:
- The generated PNG file will contain the height map as a grayscale image, where lighter colors represent higher elevation and darker colors represent lower elevation.
- You can adjust the dimensions and noise parameters as needed to create different height maps.
-