Generate Repeatable PGM Noise Images with pgmnoise
You will finish with a valid PGM image filled with random brightness values, plus a repeatable command for generating the same image again. The examples use the installed Netpbm package version 2:11.05.02-1.1build1, which reports Netpbm 11.5.2.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about ten minutes. You need a shell, the netpbm package, and a directory where you can write an output file. These commands are ordinary user commands. They do not need sudo, and they do not change system configuration.
1. Check the installed command
Confirm that the executable is available and record the package version. This is a read-only checkpoint:
$ 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
The version text is diagnostic output, not an image. If the command is missing, install Netpbm through your normal package-management process. Do not continue by guessing a replacement program's options.
2. Generate a small image
The required arguments are width and height. pgmnoise writes the image to standard output, so redirect it to a new file:
$ 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. The generated file is a raw PGM image, normally identified by the P5 magic number. Inspect its header without opening the binary pixel data in a text editor:
$ head -n 3 noise-640x480.pgm
P5
640 480
255
Checkpoint: the dimensions in the header must match the two arguments, and the third line should be 255 unless you selected a different maximum value.
3. Choose the brightness range
Use -maxval when a downstream tool needs a different sample range. The default remains the useful 8-bit range, but a smaller value can make the output's brightness values easier to constrain:
$ pgmnoise 320 200 -maxval=31 > noise-5bit.pgm
$ head -n 3 noise-5bit.pgm
P5
320 200
31
The option can also be written with two hyphens and a space, as --maxval 31. The manual permits unique option abbreviations, but full option names make scripts less surprising and easier to review. Keep the value within the range accepted by your installed Netpbm build, and verify the header rather than assuming the request was applied.
4. Make the output reproducible
By default, the seed is derived from the time of day and process ID. That is suitable for making different test images, but it means two invocations should not be expected to match. Supply -randomseed when a test fixture or bug report needs the same pixels every time:
$ 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
The hash above is an example from a 64 by 48 image on this installation. Your hash should match it when the command, dimensions, maximum value and Netpbm build are the same. The seed controls the generated pixels; it does not make a different image size match an earlier file.
5. Protect an existing output
Shell redirection with > truncates its destination before pgmnoise runs. Do not point it at a useful file until you have decided that replacement is safe. Write a new file first, then replace the old one only after checking it:
$ 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
mv replaces the old path after generation has completed. That replacement is a state change, so pause before running it if another process reads the destination. If generation or validation fails, leave the original file alone and remove the .new file when you are ready. If you need an undo point, copy the original first:
$ cp --preserve=all noise-640x480.pgm noise-640x480.pgm.bak
Removing that backup is irreversible. Keep it until the replacement has been inspected by the program that will consume it.
6. Diagnose bad arguments
Width and height must be positive. A zero width is rejected before an image is produced:
$ pgmnoise 0 480 > rejected.pgm
pgmnoise: Width argument is zero; must be positive
Because the shell creates the redirected file before the command reports the error, check whether rejected.pgm exists and is empty before reusing that name. A safer pattern is to generate into a temporary or .new path and inspect 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
If file is not available, inspect the first three header lines and check that the file is non-empty with test -s noise.pgm.new. A successful exit status confirms that generation completed; it does not prove that a later image-processing tool accepts every PGM variant.
Done means
pgmnoiseis installed and its Netpbm version is known.- The output file has the requested width and height and begins with a valid PGM header.
- You used
-maxvalonly when a specific sample range was required. - You used
-randomseedfor repeatable test or fixture data. - A new destination or
.newfile protected any existing image from accidental truncation. - Any replacement was checked first, and a backup remains if recovery is still needed.