Individual color values of a pixel?

Post Reply
RichardB
Posts: 5
Joined: Monday 05th March 2018 12:00pm

Individual color values of a pixel?

Post by RichardB » Friday 16th March 2018 3:21am

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

stevedee
Posts: 83
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee » Friday 16th March 2018 9:48am

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.

stevedee
Posts: 83
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee » Friday 16th March 2018 12:05pm

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 973 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: 192
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Individual color values of a pixel?

Post by cogier » Friday 16th March 2018 12:54pm

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 969 times
Image_colour.tar.gz
(17.73 KiB) Downloaded 45 times
Let us know how you get on.

stevedee
Posts: 83
Joined: Monday 20th March 2017 6:06pm

Re: Individual color values of a pixel?

Post by stevedee » Friday 16th March 2018 6:40pm

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 05th March 2018 12:00pm

Re: Individual color values of a pixel?

Post by RichardB » Friday 16th March 2018 8:43pm

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: 16
Joined: Wednesday 05th April 2017 6:07pm

Re: Individual color values of a pixel?

Post by vuott » Saturday 17th March 2018 6:03pm

...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

vuott
Posts: 16
Joined: Wednesday 05th April 2017 6:07pm

Re: Individual color values of a pixel?

Post by vuott » Saturday 17th March 2018 11:36pm

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 39 times

RichardB
Posts: 5
Joined: Monday 05th March 2018 12:00pm

Re: Individual color values of a pixel?

Post by RichardB » Monday 19th March 2018 10:30am

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