Individual color values of a pixel?
Posted
#1
(In Topic #161)
Trainee
How do I extract the integer values for each color of an image pixel?
If I have an image called "test_image", and can read each pixel as "test_image[x,y]", how can I extract the integer value for each of the red, green and blue channels for that pixel? (The documentation is a bit vague on this subject.)
Thanks in advance.
-RB
Posted
Regular

I think you need the gb.Image.Effect component, and basically do something like:-RichardB said
…how can I extract the integer value for each of the red, green and blue channels for that pixel?
Take a look at my blog post: Captain Bodgit: Gambas: Improving the PhotoViewer
Unfortunately I've lost this project. But if I can find some time, I'll put together a simply example.
<COLOR color="#FF0000">EDIT: Sorry! On reflection this is a rubbish answer. This just gives the data required to build a histogram, whereas you want the actual RGB values for a given pixel.</COLOR>
Posted
Regular

I created a new GUI project and added a button, a text box and 3 labels to the form.
Code (gambas)
- Pixels = hImage.Pixels
- intPixie = Pixels[1]
- Label1.Text = intPixie
- Label1.Foreground = intPixie
- intPixie = Pixels[5]
- Label2.Text = intPixie
- Label2.Foreground = intPixie
- intPixie = Pixels[12]
- Label3.Text = intPixie
- Label3.Foreground = intPixie
I then used The Gimp to create a new PNG image file 100 x 100 pixels. I added three 3x3 pixel coloured blocks, red, blue & green:
This works, but note that:-
1. The red and blue colours are reversed. This reminds me that when I was using the Gambas histogram, the red & blue were the wrong way around. Benoit fixed this bug for me, but maybe the histogram was not the real problem. Perhaps the colours are incorrectly classified or something.
2. The values returned can be entered into your calculator app (Linux Galculator) and converted to Hex for comparison with the colours in Gimp.
3. The Image.Pixels method creates a simple 1D array of the image (so index is 0-9999 for a 100 x 100 image). Why doesn't it create a 2D array like image[x,y] ?
Posted
Guru

Let us know how you get on.
Posted
Regular

Since green is in the middle (i.e. 00ff00) it is interpreted correctly.
I think this is a Gambas bug. Does anyone have another theory?
You can see the Gambas image format by using: Image.Format (e.g. in my code example: Label4.Text = hImage.Format).
Posted
Trainee
-RB
Posted
Regular

Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Regular

Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Trainee
-RB
1 guest and 0 members have just viewed this.




