Generating a text matrix with pixel values
Generating a text matrix with pixel values
I am a newbie and have gotten lost looking to see whether this can be done. I found the following forum thread, which is related: viewtopic.php?f=1&t=14646
I would like to generate a text file with the pixel colors or (preferred) grayscale intensities in a matrix corresponding to the pixel positions. That is a table of pixel values where the position in the table corresponds to the position of the pixel in the image.
I am not sure that ImageMagick can do this. Can it? Can someone give me a pointer as to where to look?
Thanks!
I would like to generate a text file with the pixel colors or (preferred) grayscale intensities in a matrix corresponding to the pixel positions. That is a table of pixel values where the position in the table corresponds to the position of the pixel in the image.
I am not sure that ImageMagick can do this. Can it? Can someone give me a pointer as to where to look?
Thanks!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
convert image txt:-
will list the text data to the terminal
convert image image.txt
will create a text file.
The first two columns are the pixel x,y coords and the rest are its color values in differnent forms.
see http://www.imagemagick.org/Usage/files/#txt
will list the text data to the terminal
convert image image.txt
will create a text file.
The first two columns are the pixel x,y coords and the rest are its color values in differnent forms.
see http://www.imagemagick.org/Usage/files/#txt
Re: Generating a text matrix with pixel values
Thank you fmw42!
Your previous posting (my reference) was the one item I had found that addressed the issue. I did not understand the significance of the "-" vs. using a .txt filename, that is helpful. (I was going to use a DOS pipe to generate the file.)
Is there any way to get this in a tabular format from ImageMagick as opposed to a one pixel per line list? For a 40x30 image, I would like to get a table of entries that are 40 across by 30 high (probably with a multiplier on that to allow for the fact that the color values are returned as 3 3-digit numbers). If not, I will need to manipulate the results in Excel or something similar. I am using the Windows version, so the unix commands are not available (or intelligible to me).
In reading the docs, I think I saw an "i" parameter that would output greyscale intensity instead of the rgb triad. Is that true? Could you point me at that?
I feel that I missed some basic background documentation/explanation of the commands. I base that on failing to know the output control that you cite. Is there a good place to read for a basic understanding along those lines?
Thanks again!
Your previous posting (my reference) was the one item I had found that addressed the issue. I did not understand the significance of the "-" vs. using a .txt filename, that is helpful. (I was going to use a DOS pipe to generate the file.)
Is there any way to get this in a tabular format from ImageMagick as opposed to a one pixel per line list? For a 40x30 image, I would like to get a table of entries that are 40 across by 30 high (probably with a multiplier on that to allow for the fact that the color values are returned as 3 3-digit numbers). If not, I will need to manipulate the results in Excel or something similar. I am using the Windows version, so the unix commands are not available (or intelligible to me).
In reading the docs, I think I saw an "i" parameter that would output greyscale intensity instead of the rgb triad. Is that true? Could you point me at that?
I feel that I missed some basic background documentation/explanation of the commands. I base that on failing to know the output control that you cite. Is there a good place to read for a basic understanding along those lines?
Thanks again!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
no way I know of to have IM format it for you to an array 40x30. if you want grayscale values rather than color use
convert image -colorspace gray image.txt
If your image is 40x30 pixels dimension, then you can convert to NetPBM (pgm) text/plain format and it will likely be in the 40x30 array with a few lines of header which can be stripped out leaving just the array.
see http://netpbm.sourceforge.net/doc/ and http://netpbm.sourceforge.net/doc/pgm.html
I 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.
convert image -colorspace gray image.txt
If your image is 40x30 pixels dimension, then you can convert to NetPBM (pgm) text/plain format and it will likely be in the 40x30 array with a few lines of header which can be stripped out leaving just the array.
see http://netpbm.sourceforge.net/doc/ and http://netpbm.sourceforge.net/doc/pgm.html
I 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.
Re: Generating a text matrix with pixel values
Thanks again for your help.
I was totally unaware of the NetPBM set of programs. I will look at this further and see if either approach makes sense in the overall scheme of things.
I appreciate your guidance.
JK
I was totally unaware of the NetPBM set of programs. I will look at this further and see if either approach makes sense in the overall scheme of things.
I appreciate your guidance.
JK
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
Here is some IM and unix code that will do the job. My example takes a 10x10 set of pixels from the top left of the rose image and displays it. I am not a Windows programmer so don't know how to convert to Windows. But see http://www.imagemagick.org/Usage/windows/
listArr=(`convert rose:[10x10+0+0] -colorspace gray txt:- | tail -n 100 |\
tr -cs '0-9\012' ' ' | cut -d\ -f3`)
num="${#listArr[*]}"
j=-1
tableArr=""
for ((i=0;i<$num;i++))
do
k=$(($i+1))
test=$(($k % 10))
if [ $test -eq 1 ]; then
j=$(($j+1))
tableArr[$j]=""
fi
tableArr[$j]="${tableArr[$j]} ${listArr[$i]}"
done
for ((i=0;i<10;i++))
do
j=$(($i+1))
echo "$j ${tableArr[$i]}"
done
1 47 48 51 52 52 52 50 51 50 50
2 46 47 49 50 51 50 49 51 51 51
3 44 44 47 47 45 46 45 46 45 46
4 45 45 45 45 43 43 43 43 44 44
5 46 46 46 46 43 43 42 39 42 43
6 47 47 48 49 49 48 46 43 43 42
7 50 51 51 54 54 52 50 47 49 46
8 55 55 56 58 61 60 59 56 56 54
9 56 57 61 63 64 66 65 65 65 62
10 54 55 60 64 66 68 68 69 68 63
The first number in each row is the row number the rest are graylevel values in range 0-255
listArr=(`convert rose:[10x10+0+0] -colorspace gray txt:- | tail -n 100 |\
tr -cs '0-9\012' ' ' | cut -d\ -f3`)
num="${#listArr[*]}"
j=-1
tableArr=""
for ((i=0;i<$num;i++))
do
k=$(($i+1))
test=$(($k % 10))
if [ $test -eq 1 ]; then
j=$(($j+1))
tableArr[$j]=""
fi
tableArr[$j]="${tableArr[$j]} ${listArr[$i]}"
done
for ((i=0;i<10;i++))
do
j=$(($i+1))
echo "$j ${tableArr[$i]}"
done
1 47 48 51 52 52 52 50 51 50 50
2 46 47 49 50 51 50 49 51 51 51
3 44 44 47 47 45 46 45 46 45 46
4 45 45 45 45 43 43 43 43 44 44
5 46 46 46 46 43 43 42 39 42 43
6 47 47 48 49 49 48 46 43 43 42
7 50 51 51 54 54 52 50 47 49 46
8 55 55 56 58 61 60 59 56 56 54
9 56 57 61 63 64 66 65 65 65 62
10 54 55 60 64 66 68 68 69 68 63
The first number in each row is the row number the rest are graylevel values in range 0-255
Re: Generating a text matrix with pixel values
Thanks again!
Your code pretty much leaves me in the dust and I suspect most of it is unix specific - or at least not windows friendly.
I have been trying to access that windows usage page for the last few days, it comes up blank. Yesterday I found this: http://www.unfallrekonstruktion.de/imag ... ndows.html I saw a not indicating that the primary webmaster was on some type of leave and that this page was filling in. At the moment, I can't find the note again, I thought it was on the referenced page, but I am not finding it now.
I think I will be in pretty good shape now for what I was aiming at.
Part of the problem I was having with the documentation on the site is it does not seem to present a basic introduction for the newbie. It seems to use as a starting point the changes in the system from older versions. It may be me, though, and I need to reread the docs from the beginning, now that I have a better handle on things.
JK
Your code pretty much leaves me in the dust and I suspect most of it is unix specific - or at least not windows friendly.
I have been trying to access that windows usage page for the last few days, it comes up blank. Yesterday I found this: http://www.unfallrekonstruktion.de/imag ... ndows.html I saw a not indicating that the primary webmaster was on some type of leave and that this page was filling in. At the moment, I can't find the note again, I thought it was on the referenced page, but I am not finding it now.
I think I will be in pretty good shape now for what I was aiming at.
Part of the problem I was having with the documentation on the site is it does not seem to present a basic introduction for the newbie. It seems to use as a starting point the changes in the system from older versions. It may be me, though, and I need to reread the docs from the beginning, now that I have a better handle on things.
JK
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
I am not a Windows users, but there are some on the list who could help you convert my unix example above. But the windows information page does work and is at http://www.imagemagick.org/Usage/windows/
Re: Generating a text matrix with pixel values
I understood that you were not a Windows user, and very much appreciate your help and efforts.
Regarding the link, before posting this I checked the page again. All of the other pages on the imagemagick.org site work fine for me. That one comes up blank in my AOL browser and in IE7, repeatedly. However, I also tried Firefox Portable v2.0, just to give it all the chances I could and it works there. I am sure there's something nasty that could be said about the one Windows specific page on the website not working in the Windows brand browser.
Giving the page a very brief visual scan says it's the same as the one at the link I provided.
Thanks!
JK
Regarding the link, before posting this I checked the page again. All of the other pages on the imagemagick.org site work fine for me. That one comes up blank in my AOL browser and in IE7, repeatedly. However, I also tried Firefox Portable v2.0, just to give it all the chances I could and it works there. I am sure there's something nasty that could be said about the one Windows specific page on the website not working in the Windows brand browser.

Giving the page a very brief visual scan says it's the same as the one at the link I provided.
Thanks!
JK
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
There were some known issues with that page when converted by Anthony from the person who supplied it. But Anthony is out-of-country and won't be able to fix it properly until he can get back to a place internet connection. Hopefully one of the Windows users can step in and help you convert my code to something Window's compatible or someone will answer my post about converting images to NetPBM pgm plain text format at viewtopic.php?f=2&t=15321
This line of code:
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.
This line of code:
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
see viewtopic.php?f=2&t=15321&p=54093#p54093 which explains how to get IM to convert to plain text PGM format. But unfortunately, it does not seem to group the rows of pixel data. So one is left with some kind of conversion of my unix to Windows Batch script.
However, it does make it easier to get a simple list of graylevel values from which you can then loop to arrange in rows of the desired length.
Here is the image to pgm convert command and then open the file in a text editor and you will see
convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 rose10.pgm
or simply the following to have it listed to the terminal
convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 PGM:-
P2
10 10
255
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
However, it does make it easier to get a simple list of graylevel values from which you can then loop to arrange in rows of the desired length.
Here is the image to pgm convert command and then open the file in a text editor and you will see
convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 rose10.pgm
or simply the following to have it listed to the terminal
convert rose:[10x10+0+0] -colorspace gray -compress none -depth 8 PGM:-
P2
10 10
255
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
Re: Generating a text matrix with pixel values
That's very helpful! I see you reached out for help from others as well. 
It looks like this gives a "standard" record line of (just under) 80 characters. I could probably work with that on a line by line basis if I needed to.
You asked about "printf" in your message on the other thread. I do not see that in the IM commands or options. Is it a unix command?
I expect that I could manage some of the ascii file manipulations that you are doing with unix commands in either DOS commands (windows) or a fairly straightforward Basic program or similar.
Thanks again!!
JK

It looks like this gives a "standard" record line of (just under) 80 characters. I could probably work with that on a line by line basis if I needed to.
You asked about "printf" in your message on the other thread. I do not see that in the IM commands or options. Is it a unix command?
I expect that I could manage some of the ascii file manipulations that you are doing with unix commands in either DOS commands (windows) or a fairly straightforward Basic program or similar.
Thanks again!!
JK
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
printf is a unix, c command as far as I know. It is not an IM command.
From the NetPBM docs at http://netpbm.sourceforge.net/doc/pgm.html
it says line max is 70 characters
From the NetPBM docs at http://netpbm.sourceforge.net/doc/pgm.html
it says line max is 70 characters
Re: Generating a text matrix with pixel values
Hey JK,
I was wondering if you figured this out? I am trying to do exactly what you were trying to accomplish here and would really appreciate a little help as I am a little confused.
In fact any help would be much appreciated.
Barry
I was wondering if you figured this out? I am trying to do exactly what you were trying to accomplish here and would really appreciate a little help as I am a little confused.
In fact any help would be much appreciated.
Barry
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Generating a text matrix with pixel values
JKEngineer wrote:
You asked about "printf" in your message on the other thread. I do not see that in the IM commands or options. Is it a unix command?
JK
I have not used this, but 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.
This is the only example I have of using it that was provided by Anthony.
convert -size 100x100 label:A -print '%wx%h %[width]x%[height] %g\n' \
-trim -print '%wx%h %[width]x%[height] %g\n'\
+repage -print '%wx%h %[width]x%[height] %g\n' \
-extent 90x90 -print '%wx%h %[width]x%[height] %g\n' null:
100x100 100x100 100x100+0+0
60x63 100x100 100x100+0+17
60x63 100x100 0x0+0+0
90x90 100x100 0x0+0+0
So I am not sure it will help in doing formatting.