PPM example

edit

{{Unencyclopedic}}

P3
#the P3 means colors are in ascii, then 3 columns and 2 rows, then 255 for max color, then RGB triplets
3 2
255
255 0 0
0 255 0
0 0 255
255 255 0
255 255 255
0 0 0

The image (expanded):  

The above image was expanded without interpolation, using the imagemagick command

convert -sample %6400 tiny6pixel.ppm tiny6pixel.png

The P6 format of the same image will store each color component of each pixel with one byte (thus three bytes per pixel) in the order Red, Green then Blue. The file will be smaller but the color information will not be readable by humans:

P6
#any comment string
3 2
255
!@#$%^&*()_+|{}:"<

The PPM format is not compressed and thus requires more space and bandwidth than a compressed format would require. For example, the above 192x128 PNG image has a file size of 187 bytes. When converted to a 192x128 PPM image, the file size is 73848 bytes. The PPM format is generally an intermediate format used for image work before converting to a more efficient format, for example the PNG (Portable Network Graphics) format. The PPM format can be converted to PNG without loss of information.

The PPM format is certainly simple to write from scratch. The following Python code makes the above example image. It can be adapted to making useful images by reading or constructing an array of numerical data, and programming a conversion of the data to color triplets.

#!/usr/bin/python
triplets=[ [255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 255, 255], [0, 0, 0] ]
width=3; height=2
comment='any comment string'
ftype='P6' #use 'P3' for ascii, 'P6' for binary

ppmfile=open('testimage.ppm','wb')
ppmfile.write("%s\n" % (ftype)) 
ppmfile.write("#%s\n" % comment ) 
ppmfile.write("%d %d\n" % (width, height)) 
ppmfile.write("255\n")

if ftype=='P3':
    for red,green,blue in triplets:
        ppmfile.write("%d %d %d\n" % (red,green,blue)) 
elif ftype=='P6': #print 1 byte per color
    for red,green,blue in triplets:
        ppmfile.write("%c%c%c" % (red,green,blue))

ppmfile.close()

Similar code in C :

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define xor(a, b) (((!a && b)) || (a && !b))

int main (int argc, char * argv[]){

   /* Declare the variables */
   FILE* ppmFile;
   int columns, rows, numberOfPixelsInPicture;

   /* Check the input arguments */
   if(argc != 4){
      printf("Usage: %s cols rows fname.ppm\n\n", *argv);
      return 1;
   }

   /* Open the file and write the first 4 lines to it */
   ppmFile = fopen(argv[3], "w");
   columns = atoi(argv[1]);
   rows = atoi(argv[2]);
   numberOfPixelsInPicture = rows * columns;
   fprintf(ppmFile, "P3\n%d\n%d\n255\n", columns, rows);

   int m, n;
   for(m=0;m<rows;m++) {
      for(n=0;n<columns;n++) {
         /* Set the pixels in the bottom left of the picture a green-yellow color */
         if(m>n) {
            fprintf(ppmFile, "%d 255 %d\n", (256*m)/rows, (256*n)/columns);
         } else if(xor((m/10)%2,(n/10)%2)) {
            /* Create a checkerboard pattern in the upper-right of the picture */
            /* Create the blue-red pixels in the checkerboard pattern */
            fprintf(ppmFile, "%d 0 %d\n", (255*m*n)/numberOfPixelsInPicture, 255 -(255*m*n)/numberOfPixelsInPicture);
         } else {
            /* Create the black pixels in the checkerboard pattern */
            fprintf(ppmFile, "0 0 0\n");
         }
      }
      /* Print a blank line between rows */
      fprintf(ppmFile, "\n");
   }

   fclose(ppmFile);
   return 0;
}