Pages

Friday, March 9, 2012

Writing PGM files in Python, C++, and C


Below are short codes that write gray scale PGM files in their respective languages without using any toolboxes like IPL. The output should be a salt-and-pepper image that can be  viewed in an image viewer. 


Python Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import array
import random
import sys

# define the width  (columns) and height (rows) of your image
width = 480
height = 640

# declare 1-d array of unsigned char and assign it random values
buff=array.array('B')

for i in range(0, width*height):
  buff.append(random.randint(0,255))


# open file for writing 
filename = 'x.pgm'

try:
  fout=open(filename, 'wb')
except IOError, er:
  print “Cannot open file “, filename, “Exiting … \n“, er
  sys.exit()


# define PGM Header
pgmHeader = 'P5' + '\n' + str(width) + '  ' + str(height) + '  ' + str(255) + '\n'

# write the header to the file
fout.write(pgmHeader)

# write the data to the file 
buff.tofile(fout)

# close the file
fout.close()
Successfully tested in Ubuntu 10.10 and Python 2.6.6


C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

typedef unsigned char uchar;

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

{ 
  string filename = argv[1];

  // define image dimensions
  unsigned short width   =  480;
  unsigned short height  =  640;

  // allocate memory for your data
  unsigned char *buff = new unsigned char[width*height*sizeof(uchar)];

  for (int i = 0; i < width*height; i++)
    buff[i] = rand() % 256;


  // output file streams
  ofstream fout (filename.c_str());

  if (!fout.is_open())
  {
     cout << "Can't open output file"  << filename << endl;
     exit(1);
  }

  // write the header
  fout << "P5\n" << width << " " << height << " 255\n";

  // write the data
  fout.write((char *)buff, width*height*sizeof(uchar));

  // close the stream
  fout.close();

  // free memory
  delete[] buff;

  // return 
  return 0;

}
Successfully tested in Ubuntu 10.10 and g++ 4.4.5


C Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>

typedef unsigned char uchar;

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

   /* define image dimensions */
   unsigned short width   =  480;
   unsigned short height  =  640;


   /* allocate memory for your data */
   unsigned char *buff = (uchar *) malloc (width*height*sizeof(uchar));
   

   /* assign random data to the array */
   int i;
   for (i = 0; i < width*height; i++)
      buff[i] = rand() % 256;


   /* open output file */
   FILE* image = fopen(argv[1], "wb");
   if (image == NULL)
   {
      fprintf(stderr, "Can't open output file %s!\n", argv[1]);
      exit(1);
   }


   /* write the header */
   fprintf(image, "P5\n%u %u 255\n", width, height);


   /* write the array */
   fwrite(buff, 1, width*height*sizeof(unsigned char), image);


   /* close the file */
   fclose(image);


   /* free memory */
   free(buff);


   /* return */
   return 0;
}
Successfully tested in Ubuntu 10.10 and gcc 4.4.5




No comments:

Post a Comment