Create a Solid-Colour PPM with ppmmake
By the end of this guide you will have a valid PPM image filled with one colour, with dimensions and sample range you chose yourself. ppmmake writes the image to standard output, so you can save it, inspect it or pass it to another Netpbm tool.
The route
Jump straight to the step you need, or tick off Done means at the end.
Before you start
You need a Linux shell, the Netpbm package and enough space for the output file. No root access is needed for the examples. Allow about five minutes: the command is small, but PPM files are deliberately simple and can be much larger than compressed image formats.
This guide was checked with Debian's netpbm package version 2:11.05.02-1.1build1, providing Netpbm 11.5.2. Check the executable on your own machine before relying on version-specific behaviour:
command -v ppmmake
ppmmake --version
Expected version output includes a line like this:
ppmmake: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
1. Generate a red test image
The command takes three positional arguments: a colour, a width and a height. Redirect standard output to a file. The redirection is the part that makes a persistent image; without it, binary data will be sent to your terminal.
ppmmake red 50 50 > red-50x50.ppm
The file should now exist and be 50 pixels wide by 50 pixels high. The default maximum sample value is 255, which is the usual 8-bit-per-channel range for a PPM.
Checkpoint
Confirm the file type and size before opening it in another program.
file red-50x50.ppm
wc -c red-50x50.ppm
On a typical system, file identifies it as a Netpbm image or a portable pixmap. The byte count is not simply width times height: the file includes a header and three colour samples for every pixel.
2. Understand what was written
For the normal case, ppmmake produces a binary PPM, whose magic number is P6. Its header contains the magic number, dimensions and maximum sample value, followed by red, green and blue bytes for each pixel. A 50 by 50 image therefore contains 7,500 sample bytes after the header.
Inspect just the header. This does not modify the image:
head -c 20 red-50x50.ppm | od -An -c
Look for the readable sequence P6, then 50 50, then 255, separated by newlines. The remaining bytes are binary pixel data, so avoid using a text editor to inspect the whole file.
3. Use an explicit colour and smaller sample range
Named colours such as red are convenient. For an exact RGB value, use Netpbm's rgb: notation. This example creates a pale red image and limits each channel to values from 0 through 5:
ppmmake rgb:ff/80/80 50 100 -maxval=5 > pale-red.ppm
The command's order is significant: the colour comes first, followed by width and height. The option may be written with an equals sign as above. The manpage also permits whitespace between the option and its value, and permits a double hyphen. Keeping the documented long form makes scripts easier to read.
Verify the requested header:
head -c 20 pale-red.ppm | od -An -c
You should see P6, 50 100 and 5. The lower maxval is not a quality setting in the same sense as image dimensions. It changes the numeric range used to store each channel, which can matter to a downstream Netpbm operation or to a program that expects a particular sample range.
4. Put the output into a pipeline
Because the image goes to standard output, a shell pipeline can hand it to another program. Keep the binary stream away from diagnostic text and from the terminal. For example, save a generated image while capturing the command's normal error stream separately:
ppmmake blue 320 200 > blue-320x200.ppm 2> ppmmake.log
test -s blue-320x200.ppm
cat ppmmake.log
A successful run normally leaves an empty log and the test command exits successfully. If you redirect both output streams into the image, diagnostic text can corrupt the PPM. Do not use 2>&1 when the destination is an image file.
Common traps and recovery
- Terminal garbage: forgetting
> output.ppmwrites binary data to the terminal. Press Ctrl-C if it is still running, then repeat the command with a file redirection. - Unexpected dimensions: width and height are positional decimal arguments. Recheck their order and inspect the header instead of guessing from a file viewer.
- Colour spelling: a colour name must be accepted by Netpbm's colour parser. If a name fails, use an explicit
rgb:value such asrgb:00/80/ff. - Corrupted output: generate a new file rather than appending with
>>. If you need to replace an existing image, check the filename first. The replacement is irreversible unless you have a copy. - Huge files: PPM is uncompressed. A large width and height can consume substantial disk space and processing time. Start with a small test image, then check available space before scaling up.
If a bad file was created by one of these examples, remove only that known output after checking the name:
ls -l -- red-50x50.ppm pale-red.ppm blue-320x200.ppm
rm -- red-50x50.ppm pale-red.ppm blue-320x200.ppm
The rm command changes state and has no undo. Do not paste it if any filename is valuable, and do not add a wildcard. Root privileges are not appropriate here.
Done means
- You can identify the installed Netpbm version with
ppmmake --version. - You can create a solid-colour PPM with a chosen width and height.
- You can set
-maxvalwhen a downstream tool requires a different sample range. - You have verified the
P6header without treating binary data as text. - You keep standard output in the image file and diagnostics separate.