Page 2 of 2

Re: Generating a text matrix with pixel values

Posted: 2010-06-01T19:32:02-07:00
by anthony
fmw42 wrote:nI am not an expert on the conversion to pgm, but it should be possible to convert to the text/plain format. However, when I just do

convert image -colorspace gray P2:image.pgm

Unfortunately, I get the binary format and the not the text/plain format.
Add -compress none and you will get the text sub-format. The "P2:" is not needed.

Or filter the PGM though the NetPBM utility "pnmnoraw" to make it text.

Re: Generating a text matrix with pixel values

Posted: 2010-06-01T19:36:55-07:00
by anthony
fmw42 wrote: listArr=(`convert rose:[10x10+0+0] -colorspace gray txt:- | tail -n 100 |\
tr -cs '0-9\012' ' ' | cut -d\ -f3`)

basically converts a 10x10 set of pixels to colorspace gray and then outputs it to the IM txt format. The tail -n 100 removes the first header line and leaves the remaining 100 lines containing the location of each pixel and all the color values in different formats. The tr part removes all characters but numbers and spaces. The cut line simply extracts the graylevel value. So the result is a list, actually in this case the (..) makes it into an array of graylevel values.

The remaining lines of code loop over the array and format it to 10 rows of 10 values with a row number at the beginning.
It is better to use "tail -n +2" to remove just the first line, rather than rely on the count of the number of pixels.

Re: Generating a text matrix with pixel values

Posted: 2010-06-01T20:02:04-07:00
by anthony
Note for Windows Users All these utilities is available under Windows, including a BASH shell, using the cygwin package.

Okay you want an array, try this in unix command

First get the PGM output as one number per line (after striping of the header)

Code: Select all

convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 PGM:- | 
       tail -n +4 | tr -s ' \012' '\012'
Now make it 10 numbers per row...

Code: Select all

   ... | paste - - - - - - - - - -

Code: Select all

47      48      51      52      52      52      50      51      50      50
46      47      49      50      51      50      49      51      51      51
44      44      47      47      45      46      45      46      45      46
45      45      45      45      43      43      43      43      44      44
46      46      46      46      43      43      42      39      42      43
47      47      48      49      49      48      46      43      43      42
50      51      51      54      54      52      50      47      49      46
55      55      56      58      61      60      59      56      56      54
56      57      61      63      64      66      65      65      65      62
54      55      60      64      66      68      68      69      68      63
this will let you adjust the number of columns. the number is 8 * pixels_per_row

Code: Select all

   ... | column -x -c80 
If you don't mind the data 'transposed' diagonally you can use a VERY old command
called pr

Code: Select all

   ... | pr -t -10 -l10 

Code: Select all

47     46     44     45     46     47     50     55     56     54
48     47     44     45     46     47     51     55     57     55
51     49     47     45     46     48     51     56     61     60
52     50     47     45     46     49     54     58     63     64
52     51     45     43     43     49     54     61     64     66
52     50     46     43     43     48     52     60     66     68
50     49     45     43     42     46     50     59     65     68
51     51     46     43     39     43     47     56     65     69
50     51     45     44     42     43     49     56     65     68
50     51     46     44     43     42     46     54     62     63
the first number in pr is number of 'lines' (image width as image is transposed)
the second the number of columns per line (image height)
You could transpose the image before outputting so 'pr' can transpose back correctly.

Code: Select all

convert rose:[10x10+0+0] -colorspace gray -transpose -compress none -depth 8 PGM:- | 
         tail -n +4 | tr -s ' \012' '\012' | pr -t -10 -l10

Code: Select all

47     48     51     52     52     52     50     51     50     50
46     47     49     50     51     50     49     51     51     51
44     44     47     47     45     46     45     46     45     46
45     45     45     45     43     43     43     43     44     44
46     46     46     46     43     43     42     39     42     43
47     47     48     49     49     48     46     43     43     42
50     51     51     54     54     52     50     47     49     46
55     55     56     58     61     60     59     56     56     54
56     57     61     63     64     66     65     65     65     62
54     55     60     64     66     68     68     69     68     63
Their are lots of solutions in UNIX. Basically because UNIX has been around longer and comes of the days before fancy iterative graphics!

Re: Generating a text matrix with pixel values

Posted: 2010-06-01T20:06:31-07:00
by anthony
fmw42 wrote:The IM -print may behave more like printf. You can try that. see http://www.imagemagick.org/script/comma ... .php#print but it does not say much about formatting.
IM -print has no formatting options! At least not at this time. You get what you get.
The only control you do have is for -precision whcih is number of floating point digits to include. But not all IM output uses this yet (for example -verbose -distort does not use this yet though 'showkernel' with morphology kernel output does, but this will change).