Seamless Texture — Python source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the Python implementation — the same logic the interactive tool runs, in a shareable, citable form.
# seamless-texture — tileable-texture math, Python port: wrap-blend weights,
# integer lattice hash, wrapping value noise, torus Worley (stdlib only).
# Python port of the pure src/lib/seamless.ts (the TS lib has no canvas or
# DOM dependency at all); the browser island owns all drawing. The 32-bit
# avalanche is emulated with masks — Python ints are arbitrary precision.
from __future__ import annotations
import math
from dataclasses import dataclass
def _imul(a: int, b: int) -> int:
"""32-bit wrapping multiplication — the Math.imul analogue."""
r = (a & 0xFFFFFFFF) * (b & 0xFFFFFFFF) & 0xFFFFFFFF
return r - 0x100000000 if r >= 0x80000000 else r
def _shr(h: int, n: int) -> int:
"""The JS `h >>> n`: a logical right shift of the int32 bit pattern."""
return (h & 0xFFFFFFFF) >> n
def blend_weight(x: float, y: float, w: float, h: float, feather: float) -> float:
"""Cross-fade weight for wrap-blending a texture's seams.
1 in the interior, falling linearly to 0 across `feather` pixels
approaching each wrapped edge (axis weights multiplied, clamped 0..1).
`feather <= 0` disables the ramp.
"""
if not feather > 0:
return 1.0
fx = min(x, w - x) / feather
fy = min(y, h - y) / feather
return min(1.0, max(0.0, fx)) * min(1.0, max(0.0, fy))
def hash2(x: int, y: int, seed: int) -> float:
"""Deterministic 0..1 hash of an integer lattice point.
32-bit integer avalanche — h = x·0x9E3779B1 ^ y·0x85EBCA77 ^
seed·0xC2B2AE35 (truncated to int32), then three xor-shift/multiply
rounds; the unsigned result is scaled by 1/2³². Same (x, y, seed) →
same value on every platform and run.
"""
h = _imul(x, 0x9E3779B1) ^ _imul(y, 0x85EBCA77) ^ _imul(seed, 0xC2B2AE35)
h ^= _shr(h, 15)
h = _imul(h, 0x2C1B3C6D)
h ^= _shr(h, 12)
h = _imul(h, 0x297A2D39)
h ^= _shr(h, 15)
return (h & 0xFFFFFFFF) / 4294967296.0
def _js_round(v: float) -> float:
"""JS Math.round (half up) — Python's round() is banker's rounding."""
return math.floor(v + 0.5)
@dataclass
class NoiseOptions:
"""Mirrors the TS NoiseOptions.
scale: tile period in pixels — value_noise(x + scale, y) == value_noise(x, y).
octaves: >= 1; each extra octave doubles frequency and halves amplitude.
"""
seed: int
scale: float
octaves: int = 1
def value_noise(x: float, y: float, opts: NoiseOptions) -> float:
"""Wrapping value noise over a unit lattice; the field tiles exactly.
Octaves >= 2 sum frequency-doubled, amplitude-halved layers normalized
by total amplitude; scale < 1 snaps to 1; octaves < 1 snaps to 1.
"""
scale = max(1.0, _js_round(opts.scale))
octaves = max(1, math.floor(opts.octaves))
total = 0.0
s = 0.0
amp = 1.0
freq = 1.0
for o in range(octaves):
s += amp * _layer(x * freq, y * freq, scale * freq, opts.seed + o * 101)
total += amp
amp /= 2
freq *= 2
return s / total
def _layer(x: float, y: float, period: float, seed: int) -> float:
"""One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes."""
x0 = math.floor(x)
y0 = math.floor(y)
u = _fade(x - x0)
v = _fade(y - y0)
xa = _wrap(x0, period)
xb = _wrap(x0 + 1, period)
ya = _wrap(y0, period)
yb = _wrap(y0 + 1, period)
top = _lerp(hash2(int(xa), int(ya), seed), hash2(int(xb), int(ya), seed), u)
bottom = _lerp(hash2(int(xa), int(yb), seed), hash2(int(xb), int(yb), seed), u)
return _lerp(top, bottom, v)
def _wrap(i: float, period: float) -> float:
"""Positive modulo — lattice indices wrap onto the torus of `period` cells."""
return (i % period + period) % period
def _fade(t: float) -> float:
"""Quintic smoothstep fade — C2-continuous interpolation curve."""
return t * t * t * (t * (t * 6 - 15) + 10)
def _lerp(a: float, b: float, t: float) -> float:
return a + (b - a) * t
@dataclass
class WorleyOptions:
"""Mirrors the TS WorleyOptions: number of hashed feature points (>= 1)."""
seed: int
points: float
@dataclass
class WorleyResult:
"""Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points."""
d1: float
d2: float
def worley(x: float, y: float, opts: WorleyOptions) -> WorleyResult:
"""Wrapping (torus-metric) F1-F2 field; tiles exactly with period 1.
Each axis distance wraps via min(d, 1-d); raw torus distances top out at
the half-diagonal √½, which maps to 1. Fewer than one point returns the
all-max {1, 1}; exactly one point leaves d2 at the maximum.
"""
# Wrap the query onto the unit tile first so shifted queries are exact.
wx = x - math.floor(x)
wy = y - math.floor(y)
n = math.floor(opts.points)
d1 = 1.0
d2 = 1.0
for i in range(n):
px = hash2(i, 0, opts.seed)
py = hash2(i, 1, opts.seed)
dx = abs(wx - px)
dx = min(dx, 1 - dx)
dy = abs(wy - py)
dy = min(dy, 1 - dy)
d = math.sqrt(dx * dx + dy * dy) * math.sqrt(2)
if d < d1:
d2 = d1
d1 = d
elif d < d2:
d2 = d
return WorleyResult(d1, d2)
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →