Skip to content
donkamonka17-specPublic

About

Tileable, seamless woven-cloth texture generator in Python: seeded PNG tiles.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

63 Commits

Folders and files

Repository files navigation

loomwork

six woven cloth tiles, seeds 1 to 6, madder palette

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 N whole 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)
  • --palette indigo (default), madder, moss, ember or slate (lowercase); or your own 2 to 5 colours as #rrggbb separated by commas, all different, e.g. --palette "#102030,#a0b0c0,#ff8800"
  • --size S side length in pixels, 8 to 4096 (default 256)
  • --palette-from IMAGE take 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 as palette: #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.py checks the option on flat-colour images made on the spot
  • --periodic a 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 W pattern family: auto (default: the seed picks one of three, as before, so existing seeds give the same files), twill, basket or mixed. 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 FILE where to write the image (required). A name ending in .svg (any case) writes SVG; any other name writes PNG
  • --normal FILE also 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 --weave and --periodic; it is refused with --sheet, with a .svg name, and when the name equals --out or --preview. Same flags give the same bytes, and --out is 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 X slope 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 HI also 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 R also 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-radius is checked only when --ao is given, otherwise it is ignored. Same height model, so it is an approximation of shading in the weave, not a light simulation.
  • --height FILE also 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 FILE also 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 N instead of one cloth, write a contact sheet of N cloths (1 to 64) in a grid: seeds --seed to --seed+N-1, same palette and size, with the seed printed under each tile. --seed must be 0 or more, --preview cannot 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

Checking another generator

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

Gallery

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).

Image Seed Command
seed 1 1 python3 loomwork.py --seed 1 --palette indigo --size 256 --out gallery/cloth_01.png
seed 2 2 python3 loomwork.py --seed 2 --palette madder --size 256 --out gallery/cloth_02.png
seed 3 3 python3 loomwork.py --seed 3 --palette moss --size 256 --out gallery/cloth_03.png
seed 4 4 python3 loomwork.py --seed 4 --palette ember --size 256 --out gallery/cloth_04.png
seed 5 5 python3 loomwork.py --seed 5 --palette slate --size 256 --out gallery/cloth_05.png
seed 6 6 python3 loomwork.py --seed 6 --palette indigo --size 256 --out gallery/cloth_06.png
seed 7 7 python3 loomwork.py --seed 7 --palette madder --size 256 --out gallery/cloth_07.png
seed 8 8 python3 loomwork.py --seed 8 --palette moss --size 256 --out gallery/cloth_08.png
seed 9 9 python3 loomwork.py --seed 9 --palette ember --size 256 --out gallery/cloth_09.png
seed 10 10 python3 loomwork.py --seed 10 --palette slate --size 256 --out gallery/cloth_10.png
seed 11 11 python3 loomwork.py --seed 11 --palette indigo --size 256 --out gallery/cloth_11.png
seed 12 12 python3 loomwork.py --seed 12 --palette madder --size 256 --out gallery/cloth_12.png

Field note: docs/field-note.md

About

Tileable, seamless woven-cloth texture generator in Python: seeded PNG tiles.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages