Sunday, September 12, 2010

Rearranging output file from a PGM image conversion.

2010-09-12 Sun 11:44

convert ex-e_g.jpg[32x32+8+5] \
 -colorspace gray \
 -transpose \
 -compress none \
 -depth 8 PGM:- |\
  tail -n +4 |\
  tr -s ' \012' '\012' |\
  pr -T -s -32 -l32 \
  > ex-e_g_crop32x32.txt 

The convert command is from ImageMagick. The [32x32+0+0] crops as the image is read. The -colorspace Gray ensures that the image will be grayscale. The -compress none signals to use plain ASCII rather than a binary format. PGM output (to STDOUT) from 0 to 255 is ensured with -depth 8 PGM:-. The PGM header is removed with tail -n +4 and a single column of numbers is generated with the tr command.

The workhorse for the reformat is the pr tool which prepares file for printing. The -a puts columns across which is necessary for this flow to obtain the proper orientation. (This could have also been done with the -transpose switch to convert.) The -T ensures that no headers or footers are used, and that no page breaks are inserted. The -s switch puts tabs between values. The number of columns is specified with -32 and the number of lines with -l32.

Take note that initial tests without the -s command yielded files where the numbers were incorrect. This seems to have been due to something related to the width being too narrow, but the -s set the width to 512. The pr command does have the --width switch, but it is currently unclear how to calculate the exact width needed for the particular crop size.

No comments:

Post a Comment