Home / Alt manpages / pgmnoise(1)

  • pgmnoise(1)
  • User command
  • linux

Generate Repeatable PGM Noise Images with pgmnoise

pgmnoise fills a PGM with random brightness values on demand, no camera, no scanner, no stock photo licence to check. With the right flag it will hand you the exact same pixels again tomorrow. The examples below use the installed Netpbm package version 2:11.05.02-1.1build1, reporting Netpbm 11.5.2.

Allow about ten minutes. You need a shell, the netpbm package and somewhere writable. Every command here is an ordinary user command: no sudo, no touching system configuration.

1. Check the installed command

Confirm the executable exists and note the package version, a read-only step:

$ command -v pgmnoise
/usr/bin/pgmnoise
$ pgmnoise --version 2>&1 | sed -n '1,2p'
pgmnoise: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pgmnoise: Built from source dated 2024-03-31 09:09:47
$ dpkg-query -W -f='${Package} ${Version}
' netpbm
netpbm 2:11.05.02-1.1build1

That version output is diagnostic text, not an image. If the command is missing, install Netpbm through your normal package manager rather than guessing at a substitute program's flags.

2. Generate a small image

Width and height are the only required arguments. pgmnoise writes to standard output, so redirect it:

$ pgmnoise 640 480 > noise-640x480.pgm
$ file noise-640x480.pgm
noise-640x480.pgm: Netpbm image data, size = 640 x 480, rawbits, greymap

The default maximum sample value is 255, and the file itself is a raw PGM, normally identified by its P5 magic number. Check the header without opening binary pixel data in a text editor:

$ head -n 3 noise-640x480.pgm
P5
640 480
255

Checkpoint

The header's dimensions must match your two arguments, and the third line reads 255 unless you asked for a different maximum.

3. Choose the brightness range

Set -maxval when a downstream tool expects a different sample range. The default already covers the useful 8-bit range, but a smaller value can make the noise easier to reason about:

$ pgmnoise 320 200 -maxval=31 > noise-5bit.pgm
$ head -n 3 noise-5bit.pgm
P5
320 200
31

You can also write it as --maxval 31. Netpbm allows unique option abbreviations, but full names read better in a script someone else has to review later. Check the header afterwards rather than assuming the request landed.

4. Make the output reproducible

Left alone, pgmnoise seeds itself from the time of day and the process ID, which is fine for generating varied test images but means two runs are never expected to match. Set -randomseed whenever a test fixture or bug report needs the exact same pixels twice:

$ pgmnoise 64 48 -randomseed=17 > noise-seed-17-a.pgm
$ pgmnoise 64 48 --randomseed 17 > noise-seed-17-b.pgm
$ sha256sum noise-seed-17-a.pgm noise-seed-17-b.pgm
405da723e19bc7eb4cb3211ad76a9b8b4b4a64584970aed3f4dcc7c93ec984e9  noise-seed-17-a.pgm
405da723e19bc7eb4cb3211ad76a9b8b4b4a64584970aed3f4dcc7c93ec984e9  noise-seed-17-b.pgm

That hash comes from a 64 by 48 image on this installation. Yours should match given the same command, dimensions, maximum value and Netpbm build. The seed fixes the pixels for a given size; it will not make two different sized images line up.

5. Protect an existing output

Shell redirection with > truncates its target before pgmnoise even starts, so never point it at a file you care about until you are sure replacement is safe. Write the new file first, check it, then swap it in:

$ pgmnoise 640 480 -randomseed=17 > noise-640x480.pgm.new
$ file noise-640x480.pgm.new
noise-640x480.pgm.new: Netpbm image data, size = 640 x 480, rawbits, greymap
$ mv noise-640x480.pgm.new noise-640x480.pgm

Warning

That mv replaces the old path the moment generation has finished, and that replacement is itself a state change, so pause if something else might still be reading the destination. Leave the original alone if generation or validation fails, and clean up the .new file once you are ready. For a proper undo point, copy the original first:

$ cp --preserve=all noise-640x480.pgm noise-640x480.pgm.bak

Deleting that backup is irreversible, so keep it until whatever consumes the replacement has actually inspected it.

6. Diagnose bad arguments

Width and height must both be positive. A zero width gets rejected before any image is written:

$ pgmnoise 0 480 > rejected.pgm
pgmnoise: Width argument is zero; must be positive

Because the shell creates the redirected file before pgmnoise reports that error, check whether rejected.pgm exists and is empty before reusing that filename. Safer still, generate into a temporary or .new path and check the exit status:

$ pgmnoise 640 480 -randomseed=17 > noise.pgm.new && file noise.pgm.new
noise.pgm.new: Netpbm image data, size = 640 x 480, rawbits, greymap

No file command available? Check the first three header lines by hand and confirm the file is non-empty with test -s noise.pgm.new. A clean exit status only confirms generation finished; it says nothing about whether a later tool will accept every PGM variant.

Done means

  • Command confirmed. pgmnoise is installed and its Netpbm version is known.
  • Header verified. The output begins with a valid PGM header at the width and height you asked for.
  • Range set deliberately. -maxval was only used when a specific sample range was actually required.
  • Reproducibility handled. -randomseed was used wherever a fixture or bug report needed repeatable pixels.
  • Existing files protected. A new destination or .new file kept any old image safe from truncation.
  • Replacement checked. Any swap-in was verified first, with a backup still available if you needed to recover.