Individual color values of a pixel?

Post your Gambas programming questions here.
Post Reply
RichardB
Posts: 5
Joined: Monday 5th March 2018 12:00pm

Individual color values of a pixel?

Post by RichardB »

Hi folks:

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
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee »

RichardB wrote: Friday 16th March 2018 3:21am ...how can I extract the integer value for each of the red, green and blue channels for that pixel?
I think you need the gb.Image.Effect component, and basically do something like:-

hImage = Image.Load(strFilePath)  'load photo
hHisto = hImage.Histogram()
For index = 0 To 255
    lngRedPixel = hHisto[Image.Red, index]
    lngGreenPixel = hHisto[Image.Green, index]
    lngBluePixel = hHisto[Image.Blue, index]

Take a look at my blog post: https://captainbodgit.blogspot.co.uk/20 ... iewer.html

Unfortunately I've lost this project. But if I can find some time, I'll put together a simply example.

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.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee »

OK, Take 2.

I created a new GUI project and added a button, a text box and 3 labels to the form.
Public Sub btnGetData_Click()
Dim hImage As Image
Dim Pixels As Integer[]
Dim intPixie As Integer


  hImage = Image.Load(txtImageFile.Text)  'the test image
  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

End
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:
test.png
test.png (339 Bytes) Viewed 14113 times
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] ?
User avatar
cogier
Site Admin
Posts: 1117
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Individual color values of a pixel?

Post by cogier »

I had a go at this as well and came up with the attached program
Image_colour.png
Image_colour.png (21.21 KiB) Viewed 14109 times
Image_colour.tar.gz
(17.73 KiB) Downloaded 536 times
Let us know how you get on.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee »

I think the problem with blue & red being switched is because Gambas thinks the format of my Gimp image is RGBA (which it reads low byte to high byte) where the hex value 0000ff is interpreted as red. But my Gimp image interprets this as blue (i.e. RRGGBB, high byte to low byte). See http://gambaswiki.org/wiki/doc/imageconv

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).
RichardB
Posts: 5
Joined: Monday 5th March 2018 12:00pm

Re: Individual color values of a pixel?

Post by RichardB »

Thanks for everyone's help. I'm away from my computer at the moment but I'll test out your suggestions when I'm back home.

-RB
vuott
Posts: 261
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Individual color values of a pixel?

Post by vuott »

...a very little and simple code:
Public Sub Button1_Click()

  Dim im As Image
  Dim px, n As Integer
  
   im = Image.Load("/path/of/my/file/image")

   For n = 0 To im.Pixels.Max
     px = im.Pixels[n]
     Print n, "Pixel: "; Hex(px, 6)
     Print Null, Hex(Shr(px, 16) And &FF, 2), Hex(Shr(px, 8) And &FF, 2), Hex(px And &FF, 2)
     Print
    Next
    
End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 261
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Individual color values of a pixel?

Post by vuott »

Member of Italian Gambas forum, Gianluigi, suggests this other way (look at attached file)
Attachments
GetPixColor-0.0.1.tar.gz
(356.11 KiB) Downloaded 479 times
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
RichardB
Posts: 5
Joined: Monday 5th March 2018 12:00pm

Re: Individual color values of a pixel?

Post by RichardB »

Thanks again to everyone. And particular thanks to Gianluigi for his solution (exactly what I was after!) and to vuott for posting it here.

-RB
Post Reply