Skip to main content

Python implementation for Perlin Noise with unlimited coordinates space

Project description

Smooth random noise generator
read more https://en.wikipedia.org/wiki/Perlin_noise

source code: https://github.com/salaxieb/perlin_noise

noise = PerlinNoise(octaves=3.5, seed=777)
    octaves : number of sub rectangles in each [0, 1] range
    seed : specific seed with which you want to initialize random generator     tile_sizes : tuple of ints of you want to noise seamlessly repeat itself

from perlin_noise import PerlinNoise


noise = PerlinNoise()
# accepts as argument float and/or list[float]
noise(0.5) == noise([0.5])
# --> True
# noise not limited in space dimension and seamless in any space size
noise([0.5, 0.5]) == noise([0.5, 0.5, 0, 0, 0])
# --> True
# noise can seamlessly repeat isself
noise([0.5, 0.5], tile_sizes=[2, 5]) == noise([2.5, 5.5], tile_sizes=[2, 5])
# --> True

Usage examples:

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise = PerlinNoise(octaves=10, seed=1)
xpix, ypix = 100, 100
pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]

plt.imshow(pic, cmap='gray')
plt.show()

png

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise1 = PerlinNoise(octaves=3)
noise2 = PerlinNoise(octaves=6)
noise3 = PerlinNoise(octaves=12)
noise4 = PerlinNoise(octaves=24)

xpix, ypix = 100, 100
pic = []
for i in range(xpix):
    row = []
    for j in range(ypix):
        noise_val = noise1([i/xpix, j/ypix])
        noise_val += 0.5 * noise2([i/xpix, j/ypix])
        noise_val += 0.25 * noise3([i/xpix, j/ypix])
        noise_val += 0.125 * noise4([i/xpix, j/ypix])

        row.append(noise_val)
    pic.append(row)

plt.imshow(pic, cmap='gray')
plt.show()

png

Library has a possibility to generate repetative random noise with custom tile sizes:

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise = PerlinNoise(octaves=2, seed=42)
xpix, ypix = 800, 1200
lim_x, lim_y = 6, 9
pic = [
    [
        noise([lim_x * i / xpix, lim_y * j / ypix], tile_sizes=[2, 3])
        for j in range(xpix)
    ]
    for i in range(ypix)
]

plt.imshow(pic, cmap="gray")
plt.show()

png

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise1 = PerlinNoise(octaves=1)
noise2 = PerlinNoise(octaves=3)
noise3 = PerlinNoise(octaves=6)
noise4 = PerlinNoise(octaves=12)

xpix, ypix = 800, 1200
lim_x, lim_y = 4, 6
tile_sizes = (2, 3)
pic = []
for i in range(ypix):
    row = []
    for j in range(xpix):
        noise_val = noise1([lim_x * i / xpix, lim_y * j / ypix], tile_sizes)
        noise_val += 0.5 * noise2([lim_x * i / xpix, lim_y * j / ypix], tile_sizes)
        noise_val += 0.25 * noise3([lim_x * i / xpix, lim_y * j / ypix], tile_sizes)
        noise_val += 0.125 * noise4([lim_x * i / xpix, lim_y * j / ypix], tile_sizes)

        row.append(noise_val)
    pic.append(row)

plt.imshow(pic, cmap="gray")
plt.savefig("pics/multy_noise_tiled.png", transparent=True)
plt.show()

png

for tiles to work correctly, number of octaves MUST be integer

import matplotlib.pyplot as plt
from perlin_noise import PerlinNoise

noise = PerlinNoise(octaves=2.5, seed=42)
xpix, ypix = 800, 1200
lim_x, lim_y = 6, 9
pic = [
    [
        noise([lim_x * i / xpix, lim_y * j / ypix], tile_sizes=(2, 3))
        for j in range(xpix)
    ]
    for i in range(ypix)
]

plt.imshow(pic, cmap="gray")
plt.savefig('pics/tiled_with_step.png', transparent=True)
plt.show()

png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perlin_noise-1.13.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

perlin_noise-1.13-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file perlin_noise-1.13.tar.gz.

File metadata

  • Download URL: perlin_noise-1.13.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for perlin_noise-1.13.tar.gz
Algorithm Hash digest
SHA256 0e0f074743336fbe9cf2cdbdeb74c75d43780a8b3cbc3415859cd396ab4da177
MD5 493542861756622e5e91ed80aed4bfbf
BLAKE2b-256 bd1e571287a516c1ef00d2c65316ffa39ce1078510a015fa72c27338e3ecbfff

See more details on using hashes here.

File details

Details for the file perlin_noise-1.13-py3-none-any.whl.

File metadata

  • Download URL: perlin_noise-1.13-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for perlin_noise-1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 09f274c8da38f5a2a48b1626032a1b7738990df758a3bc7549ff3464869c6ca4
MD5 da609b1495bff001c7b794fd4955dd63
BLAKE2b-256 4db2c7bd5f926f0e2cf3a4cf21c527f6d1b8f43c84bbfcd5aa32f47c7c14cc89

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page