Home / Alt manpages / pbmnoise(1)

  • pbmnoise(1)
  • User command
  • linux

Generate Reproducible PBM Noise with pbmnoise

You will finish with a PBM bitmap generated at the dimensions and black-pixel ratio you choose, with an optional seed that makes the result repeatable. The examples use the Netpbm package installed here, version 2:11.05.02-1.1build1, and write image data to files without requiring elevated privileges.

Allow about ten minutes. You need a shell, the pbmnoise command, and a directory where you can create a new output file. No service, system setting or input image is needed. This command creates a new image; it does not edit an existing one.

1. Check the installed command

Confirm that the executable and package are the ones you expect. These are ordinary read-only checks:

$ command -v pbmnoise
/usr/bin/pbmnoise
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

The command takes two positional arguments, width and height. It writes a PBM image to standard output, so a shell redirection is normally part of the command. PBM is a black-and-white bitmap format, not a greyscale or colour format.

Checkpoint: do not run a command that only prints binary image data into your terminal. Choose an output path first.

2. Generate a small test image

Start with a new filename and a modest size. This creates a 320 by 200 image using the default ratio:

$ pbmnoise 320 200 > noise-320x200.pbm
$ file noise-320x200.pbm
noise-320x200.pbm: Netpbm image data, size = 320 x 200, rawbits, bitmap

The default black-pixel probability is one half, so a reasonably large image should contain roughly equal black and white areas. The exact count varies because the pixels are random. The file wording can differ slightly between systems; check that the file is identified as a PBM bitmap with the requested dimensions.

Redirection with > truncates an existing destination before pbmnoise starts. Treat an existing image as valuable: use a new name, or enable the shell's no-clobber mode before redirecting:

$ set -o noclobber
$ pbmnoise 320 200 > noise-320x200.pbm
bash: noise-320x200.pbm: cannot overwrite existing file

If you need to replace an image, generate a separate file such as noise-320x200.new.pbm, check it, then move it over the old file deliberately. That move is the state-changing step, and there is no automatic undo.

3. Control the proportion of black pixels

Use -ratio=M/N to set the probability that each pixel is black. The denominator must be 1 or a power of two up to 65536, and the numerator cannot be greater than the denominator. For roughly one third black pixels, use a nearby permitted fraction such as 11/32:

$ pbmnoise -ratio=11/32 1200 1200 > noise-one-third.pbm
$ file noise-one-third.pbm
noise-one-third.pbm: Netpbm image data, size = 1200 x 1200, rawbits, bitmap

Use -ratio=0/1 for an entirely white result and -ratio=1/1 for an entirely black result. The installed command accepts those explicit fractions. On this installation, bare -ratio=0 and -ratio=1 are rejected as invalid ratios, even though the manual describes the endpoint values as 0 and 1. Writing the denominator avoids that version-specific trap.

A ratio controls probability, not an exact quota. On a small image, the measured proportion can be noticeably different from the requested fraction. Increase the dimensions when you need the result to be closer in aggregate.

4. Make a result repeatable

By default, the seed is derived from the time of day and process ID, so separate invocations normally produce different images. Supply -randomseed when a test, fixture or comparison needs the same bytes every time:

$ pbmnoise -randomseed=12345 320 200 > noise-seed-a.pbm
$ pbmnoise -randomseed=12345 320 200 > noise-seed-b.pbm
$ cmp --silent noise-seed-a.pbm noise-seed-b.pbm && echo 'same seed: identical output'
same seed: identical output

A seed makes the invocation repeatable on the same implementation and relevant machine behaviour. Keep the dimensions, ratio, seed and endian setting unchanged when reproducing a file. Do not use a generated test image as evidence that a production random source is unpredictable; this option deliberately makes the stream repeatable.

5. Use pack for narrow images

Without -pack, the program generates pixels in 32-bit units and discards fractional pixels left at the end of each row. -pack carries those unused pixels into the next row. It can improve performance when the width is small, at the cost of some overhead:

$ pbmnoise -pack -randomseed=7 5 300 > narrow-packed.pbm
$ file narrow-packed.pbm
narrow-packed.pbm: Netpbm image data, size = 5 x 300, rawbits, bitmap

This is an output-generation choice, not a request to resize an image. If you are comparing byte-for-byte fixtures, choose one setting and keep it fixed. The dimensions remain the values supplied on the command line.

6. Keep cross-machine tests explicit

pbmnoise internally uses random 32-bit integers. Because byte order can affect how those integers become pixel strings, the -endian option lets tests select big, little, native or swap. native is the default and leaves the machine's normal encoding unchanged.

For ordinary image generation, leave this option out. For a fixture shared between machines, record the exact Netpbm version, dimensions, ratio, seed, pack setting and endian mode alongside the expected file. Otherwise a change in one of those inputs can look like a random failure.

7. Diagnose failures without guessing

A non-zero exit status means the command did not produce a valid result for your invocation. Check the dimensions first, then the ratio spelling. This deliberately invalid example shows the kind of diagnostic to expect:

$ pbmnoise -ratio=3/10 320 200 > rejected.pbm
pbmnoise: Denominator must be a power of two.  You specified 10.
$ echo $?
1

Do not treat a file created by shell redirection as proof of success. The shell may create or truncate the destination before the program rejects an option. If a command fails, check the exit status and inspect the file before using it. Generate into a disposable new name when testing unfamiliar arguments.

For a successful result, use file to check the format and dimensions, and pass the PBM to a separate Netpbm viewer or converter if you need a visual check. The generated PBM is binary, so avoid using cat on it in a terminal.

Done means

  • pbmnoise is installed and its Netpbm version is recorded when reproducibility matters.
  • The output file has the requested width and height and is identified as a PBM bitmap.
  • The ratio uses a permitted denominator, with 0/1 or 1/1 for endpoint images.
  • A fixed seed, and optionally -pack and -endian, is recorded for repeatable tests.
  • No useful destination was overwritten accidentally by shell redirection.