Makes small, seamlessly tileable woven-cloth textures (PNG or SVG) from a seed. Same seed and options, same file, byte for byte (see Determinism below). Needs Python 3 and Pillow:
python3 -m pip install Pillow
Example:
python3 loomwork.py --seed 7 --palette indigo --size 256 --out cloth.png
Patterns: twill, basket weave or a mix (--weave). The result is a tileable texture: opposite edges match. With --periodic the tile also has no doubled line at the joins. It can also take its colours from a picture: --palette-from picks up to 5 dominant colours (median cut).
Determinism: the same seed and options give the same file, byte for byte, on the same Pillow version. CI checks 80 outputs at size 64 on Linux with Python 3.12: pixels match, and the file bytes were also identical, on Pillow 10.4.0, 11.3.0 and latest; other sizes, systems and versions are untested, see tools/test_hashes.py.
--seed Nwhole number (default 0); same seed and options give the same file, byte for byte; a negative seed is treated as its absolute value (checked only for -5 against 5)--paletteindigo (default), madder, moss, ember or slate (lowercase); or your own 2 to 5 colours as#rrggbbseparated by commas, all different, e.g.--palette "#102030,#a0b0c0,#ff8800"--size Sside length in pixels, 8 to 4096 (default 256)--palette-from IMAGEtake up to 5 dominant colours from an image file (any format Pillow reads; the alpha channel is dropped, so transparent pixels count with the colour stored for them; an animated image uses its first frame) instead of--palette; using both is an error. The colours chosen are printed aspalette: #rrggbb,...(dark to light), so you can reuse them with--palette. Colours are found by median cut on a 128-pixel thumbnail, so for photographs they can differ between Pillow versions; an image with fewer than 2 distinct colours is refused.python3 -I tools/palettecheck.pychecks the option on flat-colour images made on the spot--periodica tile that repeats with no doubled line at the joins (see below); it is still mirror-symmetric inside the tile, and the default cloth is unchanged--weave Wpattern family:auto(default: the seed picks one of three, as before, so existing seeds give the same files),twill,basketormixed. The other choices a seed makes (colours, band widths, stitch) stay the same in every family; for some seeds two weaves still give identical pixels. Every weave is mirrored, so opposite edges match (the tiling check can be run per weave:python3 -I tools/check_tiling.py --weave basket)--out FILEwhere to write the image (required). A name ending in.svg(any case) writes SVG; any other name writes PNG--normal FILEalso write a tangent-space normal map (PNG, RGB, z up) of the same cloth, for use as a texture in a renderer or game engine. Convention: red grows to the right, green grows towards the top of the image (+Y up, as in OpenGL), blue is up; a flat area is (128, 128, 255). The height behind it is a simple model of the weave (threads that are over are raised, softened by a small wrapped filter), not measured or real cloth geometry, and it ignores the colours and the stitch accent. The slopes are central differences that wrap around the edges, so the map is exactly the map of the tiled cloth and the tiles join without a jump in direction (it is not mirror-symmetric, so the equal-edges test does not apply to it). It works with--weaveand--periodic; it is refused with--sheet, with a.svgname, and when the name equals--outor--preview. Same flags give the same bytes, and--outis unchanged by it. Vectors have unit length to within quantisation (checked: worst error 1.52/255 over 6 seeds at size 64). A 1024-pixel map took about 1.5 s here; large sizes are slow (pure Python).--strength Xslope scale for--normal, above 0 up to 100 (default 4); larger is steeper. Not valid numbers (nan, inf, 0, negative) are refused.--roughness FILE,--rough-range LO HIalso write an 8-bit grayscale roughness map (PNG) from the same height field as--normal: raised threads get LO, recessed ones HI, linear between (0 to 1, LO <= HI, default 0.45 0.9). The height is a simple model of the weave, not real cloth geometry, so the values are a plausible starting point, not measurements.--ao FILE,--ao-radius Ralso write an 8-bit grayscale cavity map (PNG, a simple ambient-occlusion style term): a pixel lying below the average of its (2R+1) by (2R+1) neighbourhood is darkened in proportion to the depth, others stay white; R is 1 to 16 (default 2) and must fit inside the size;--ao-radiusis checked only when--aois given, otherwise it is ignored. Same height model, so it is an approximation of shading in the weave, not a light simulation.--height FILEalso write an 8-bit grayscale height map (PNG) of the same height field the other maps use: the model's integer height 0 to 64 maps to grey 0 to 255 (fixed scale, not stretched to the picture's own darkest and brightest), so a flat cloth gives one constant grey. Tiles exactly. A model of the weave, not real geometry.
The roughness, ao and height maps wrap around the edges, so each tiles exactly (as does --normal) (checked by comparing a 3x3 tiling of the height field with the single map; like --normal they are not mirror-symmetric, so the equal-edges test does not apply). A flat height gives a constant map, the same flags give the same bytes, and they combine with --normal, --preview and an SVG --out. These three maps are refused with --sheet, with a .svg name, with an empty name, and when two output names are the same file. The default output is unchanged. Like --normal, they are slow at large sizes (pure Python).
-
--preview FILEalso write the cloth repeated 2x2 (twice the side length; SVG if the name ends in.svg, else PNG), to see how it joins. It must be a different file from--out. Output is written all at once: if a write fails, no new file is left behind and an existing one is kept. -
--sheet Ninstead of one cloth, write a contact sheet of N cloths (1 to 64) in a grid: seeds--seedto--seed+N-1, same palette and size, with the seed printed under each tile.--seedmust be 0 or more,--previewcannot be combined with it, and the sheet may not exceed 16384 pixels on a side. Same flags give the same file; each tile is pixel-identical to the cloth made with that seed alone (the tile sits inside a light margin; the printed label uses Pillow's built-in font, so the label pixels may differ on other Pillow versions).python3 loomwork.py --seed 1 --palette madder --size 128 --sheet 6 --out sheet.png cmp sheet.png samples/sheet_madder_01-06.png
samples/sheet_madder_01-06.png is that command's output (seeds 1 to 6, madder, 128 pixels per tile).
SVG output has one <rect> per horizontal run of same-coloured pixels, shape-rendering="crispEdges", and no comments, metadata or generator tags. Same flags give the same bytes, and drawn at 1x it has the same pixels as the PNG. --sheet writes PNG only. SVG files are much larger than the PNG (about 31 KB for a 32-pixel cloth), and the size depends on the seed (measured at size 4096: about 10 MB for seed 1, about 293 MB for seed 7; at size 1024 about 2 MB for seed 2). To keep that from surprising you, loomwork refuses to write an SVG of more than about 32 MiB (33.5 million bytes; the limit is checked once per row, so a file can pass it by up to one row): it stops with exit code 2 and writes nothing, not even the other output. The 2x2 --preview counts too, and is four times the area (seed 7 at size 4096 is refused; seed 7 at size 2048 is 10 MB and works). Use a smaller --size, or a PNG name.
python3 loomwork.py --seed 1 --palette madder --size 32 --out cloth.svg
cmp cloth.svg samples/sample_01_32.svg
tools/svgcheck.py checks the 12 gallery cloths this way (rerun gives identical bytes; a small built-in rasteriser draws the rects and compares them with the PNG pixels; it regenerates its own SVGs and does not read the committed sample); tools/selfcheck.sh runs it too.
The cloth is mirrored left-right and top-bottom, so opposite edges are identical. Placed side by side, tiles join without a colour jump, but the mirroring leaves a one-pixel doubled line at each join (and, at even sizes, down the middle of each tile). Files carry only pixels, nothing else.
--periodic removes the doubled line that mirroring leaves. The mirror lines then fall on line 0 and, at even sizes, on the middle line (size // 2); at odd sizes the second one falls between the two middle lines, so those two are equal. The last line equals line 1, so no line is repeated at a join and, at even sizes, none in the middle. Neighbouring lines can still be equal by chance, as anywhere in a cloth. The first and last lines are no longer equal, so the equal-edges test does not apply; python3 -I tools/check_tiling.py --periodic checks the reflection instead (including the middle line). The same seed gives a different cloth with --periodic than without it. Without the flag, output is unchanged.
samples/sample_NN.png were made with --seed NN --palette madder --size 256.
To check one:
python3 loomwork.py --seed 1 --palette madder --size 256 --out t.png
cmp t.png samples/sample_01.png
Needs python3 and Pillow (python3 -m pip install Pillow). Output bytes can differ
between Pillow/zlib versions. What was measured: tools/selfcheck.sh passed in CI
on Pillow 10.4.0, 11.3.0 and 13.0.0 (Python 3.12), and locally on Pillow 12.1.1.
Older versions are untested.
python3 -W error::DeprecationWarning -I tools/test_normal.py tests --normal (ran OK on Pillow 12.1.1, and in-process on 13.0.0).
python3 -W error::DeprecationWarning -I tools/test_maps.py tests --roughness, --ao and --height.
For a quick check (about 7 seconds on the machine used for the test), run tools/selfcheck.sh. It regenerates
the gallery, compares the pixels, and runs a reduced tiling check. It prints
PASS or FAIL and exits 0 (pass), 1 (check failed) or 2 (cannot run, e.g. no
Pillow). It checks loomwork's own gallery, so use tools/check_tiling.py for
other generators.
To check the tiling over 600 size/palette/seed combinations (takes about 2 minutes):
python3 -I tools/check_tiling.py
It exits 0 on a pass. tools/fixtures/broken_loomwork.py is a deliberately broken
copy that it should fail on (exit 1):
python3 -I tools/check_tiling.py tools/fixtures/broken_loomwork.py
The checker's first argument can be any script that accepts
--seed N --palette P --size S --out FILE and writes a square PNG. Three small
examples are included. mirror_gen.py mirrors its pattern, so opposite edges
match; ramp_gen.py is a plain ramp, so opposite edges differ at most sizes.
wrap_gen.py wraps its coordinates periodically (mod size-1), so opposite edges
match too; the checker exits 0 on it with a failure count of 0 (600 combinations).
For a custom generator the rule is that opposite edges must match: column 0 equals column size-1, and row 0 equals row size-1.
python3 -I tools/check_tiling.py tools/examples/mirror_gen.py # exits 0
python3 -I tools/check_tiling.py tools/examples/ramp_gen.py # exits 1
python3 -I tools/check_tiling.py tools/examples/wrap_gen.py # exits 0
Each run takes a couple of minutes. The "failure count" is the number of seam
checks that failed, not the number of combinations; left-right and top-bottom
seams are counted separately, so it can exceed the combination count. The checker also tests
determinism, so a generator that depends on the clock or on random state fails.
In the checker's table, mirror-about-seam counts the seeds (out of those run)
whose column size-2 equals column 1 (and row size-2 equals row 1), meaning the
tile is a reflection about its seam; it is informational and does not affect PASS or FAIL. With --periodic the column is not meaningful (the reflection is about line 0, not about the seam).
It reports only edge matching and repeatability; a tile can pass and still look
poor. The examples ignore --palette, so the palette rows repeat one result.
Releases: https://github.com/donkamonka17-spec/loomwork/releases
Twelve cloths, seeds 1 to 12, palettes in rotation, 256 pixels. Each command, run from the repo root, should reproduce its file byte for byte (checked with cmp on Pillow 12.1.1 only; other versions were checked with tools/selfcheck.sh, which compares pixels).
Field note: docs/field-note.md












