pvigier / perlin-numpy

A fast and simple perlin noise generator using numpy
https://pvigier.github.io/2018/06/13/perlin-noise-numpy.html
MIT License
304 stars 50 forks source link

Choppy value distribution #10

Closed WilliamKappler closed 2 years ago

WilliamKappler commented 2 years ago

I noticed that the noise generated by this library shows some sort of choppiness. I'm not entirely sure if this is due to how the image is being saved vs. generated, but it's present in the example image in your readme. Here's the second readme noise map's histogram:

image

It doesn't seem to be caused by octaves. Here's a smoother one:

image

Perhaps there's rounding going on somewhere it shouldn't be?

Also, it isn't just GIMP. I tried a few different tools and they all show similar histogram anomalies.

pvigier commented 2 years ago

Hi,

I think it's due to how the image is saved. The noise was scaled on the example images, the noise is 256x256 but the part of the image that shows the noise is 369x368. So some values are repeated/interpolated.

If you use this piece of code to generate the images:

import numpy as np
import PIL.Image
from perlin_numpy import (
    generate_perlin_noise_2d, generate_fractal_noise_2d
)

np.random.seed(0)
noise = generate_perlin_noise_2d((256, 256), (8, 8))
image = PIL.Image.fromarray(((noise + 1.0) / 2.0 * 255.0).astype(np.uint8), mode='L')
image.save('noise.png')

np.random.seed(0)
noise = generate_fractal_noise_2d((256, 256), (8, 8), 5)
image = PIL.Image.fromarray(((noise + 2.0) / 4.0 * 255.0).astype(np.uint8), mode='L')
image.save('fractal.png')

You should obtain these two images which don't show any anomaly in their histograms: noise fractal