Array of 32 bit floats --> RGB Image --> wx.BitmapFromBuffer

168 views
Skip to first unread message

Steve

unread,
Jun 16, 2010, 7:56:35 PM6/16/10
to wxpytho...@googlegroups.com
Hi All,

I have several wxPython applications working which display pixel data from a camera. I have to convert the data to greyscale and then create/display the data via wx.BitmapFromBuffer and dc.DrawBitmap. I was curious if there was a quick way to keep that data so it can be displayed in color, similar to what it would look like if I displayed it in iPython with imshow? I believe I have to manually mask bits and populate the RGB array accordingly but I thought I would see if anyone has a better way of doing it.

Thanks for any help or suggestions.

Kind Regards,

Steve

Christopher Barker

unread,
Jun 16, 2010, 8:22:31 PM6/16/10
to wxpytho...@googlegroups.com
Steve wrote:
> I have several wxPython applications working which display pixel data
> from a camera. I have to convert the data to greyscale and then
> create/display the data via wx.BitmapFromBuffer and dc.DrawBitmap. I was
> curious if there was a quick way to keep that data so it can be
> displayed in color, similar to what it would look like if I displayed it
> in iPython with imshow? I believe I have to manually mask bits and
> populate the RGB array accordingly

this is quite easy to do with numpy. See notes from me in the past few
weeks about similar topics.

-CHB


--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

WinCrazy

unread,
Jun 17, 2010, 1:07:14 PM6/17/10
to wxPython-users
This is very easy to do with Pil (Python Imaging Library). Every
imaginable file-based image format can very easily be read and then be
converted to each other, which includes floating point format images.

Can you provide a floating point format sample image of any size ?

Ray Pasco

Steve

unread,
Jun 17, 2010, 2:23:50 PM6/17/10
to wxpytho...@googlegroups.com
Hi Ray,

I used PIL to create an image from my data (which I will attach - 66 x 66) and it appears to be in color but looks different than I suspect it should. Perhaps I need a different mode. The code I used was the following:

data = numpy.load ("data_float.npy")
d1 = numpy.fromstring (data, dtype = float32)
pilImage = Image.frombuffer ('RGBA', (66,66), d1, 'raw', 'RGBA', 0, 1)
imshow (pilImage)

I plan to test this with my wx application by converting the PIL image to a bitmap and displaying it with wx.DrawBitmap to see what it looks like.

Regards,

- Steve

data_float.npy

Ray Pasco

unread,
Jun 18, 2010, 5:36:09 PM6/18/10
to Wxpython Users
How about giving me an idea what the image is supposed to look like.
I have no idea how the floating point numbers were constructed.
Is this supposed to be a circular, colored scatter diagram ?


What are the r, g, b, and a *integer* values for the first non-zero pixel ?
Please convert this floating point pixel into four 8-bit non-negative integers.

I have attached a PNG file in which the alpha values (at least) seem to be correct.

Ray

Image_Float_RGBA.PNG

Ray Pasco

unread,
Jun 18, 2010, 10:26:03 PM6/18/10
to Wxpython Users
Steve,

I've been playing around withe the script snippet and the data file you provided. My basic problem is that I don't know just what is a "floating point" pixel value !  There seems to be no documentation available anywhere. It appears to be whatever the programmer wants it to mean.

For example, what does a pixel value of "-31.3153" mean ? I amd assuming that a value of "0.0" means that this pixel is transparent.  See file [ FP_data_text.txt ]. It forms a perfect 64x64 disc. Is this an output from some kind of special-purpose scanner array ?

Consider the program line:

    pilImage = Image.frombuffer ( 'RGBA', (66, 66), d1, 'raw', 'RGBA', 0, 1 )

The first arg says that you want to create an RGBA mode image. Any valid Pil mode may be used here.  The 5th parameter ( 'RGBA' ) tells Pil that the raw data order in the buffer is r-g-b-a, r-g-b-a, ...
How can this be right ?  The buffer consists of only 64x64 pixels which have a single floating point value encoding each pixel. How does one encode an RGBA pixel into a +/- floating point value ?

Anyway, I scaled the +/- data to the [0 .. 255] grey level range and made 0.0-valued pixels transparent. In reality I have no idea what the floating point values really mean. This is what I got:

Magnified 64x64 image

The patterns suggest that I am on the right track in decoding the FP values. Only the creator of the values in file [ data_float.npy ] knows what the values mean, that is, how to transform them into (back into ?) 4 RGBA-ordered image integer values.

Ray Pasco

ZZ_GREYLEVEL_TRANSPARENCY.PNG
FP_data_text.txt
Z_DECODE_NUMPY_FLOAT_IMAGE.PY
ZZ_GREYLEVEL_TRANSPARENCY.PNG

Steve

unread,
Jun 19, 2010, 2:38:35 AM6/19/10
to wxpytho...@googlegroups.com
Hi Ray,

Thanks for taking the time to look at this. The grey level range is what I get from my data too. The data is what is left over from reconstructing a wavefront after the data is collected from a CCD. For now I display everything in greyscale but I wanted to get the color going since it looked decent when I used imshow. The problem I ran into is exactly what you're running into, which is what do the values represent? 

On Fri, Jun 18, 2010 at 7:26 PM, Ray Pasco <pas...@verizon.net> wrote:
Steve,

I've been playing around withe the script snippet and the data file you provided. My basic problem is that I don't know just what is a "floating point" pixel value !  There seems to be no documentation available anywhere. It appears to be whatever the programmer wants it to mean.

As mentioned, the values are residuals from some camera data calculations  

For example, what does a pixel value of "-31.3153" mean ? I amd assuming that a value of "0.0" means that this pixel is transparent.  See file [ FP_data_text.txt ]. It forms a perfect 64x64 disc. Is this an output from some kind of special-purpose scanner array ?

Consider the program line:

    pilImage = Image.frombuffer ( 'RGBA', (66, 66), d1, 'raw', 'RGBA', 0, 1 )

The first arg says that you want to create an RGBA mode image. Any valid Pil mode may be used here.  The 5th parameter ( 'RGBA' ) tells Pil that the raw data order in the buffer is r-g-b-a, r-g-b-a, ...
How can this be right ?  The buffer consists of only 64x64 pixels which have a single floating point value encoding each pixel. How does one encode an RGBA pixel into a +/- floating point value ?

Anyway, I scaled the +/- data to the [0 .. 255] grey level range and made 0.0-valued pixels transparent. In reality I have no idea what the floating point values really mean. This is what I got:

Magnified 64x64 image

The patterns suggest that I am on the right track in decoding the FP values. Only the creator of the values in file [ data_float.npy ] knows what the values mean, that is, how to transform them into (back into ?) 4 RGBA-ordered image integer values.


ZZ_GREYLEVEL_TRANSPARENCY.PNG

Steve

unread,
Jun 19, 2010, 2:43:56 AM6/19/10
to wxpytho...@googlegroups.com
Sorry, I hit send before I meant to. :)

On Fri, Jun 18, 2010 at 11:38 PM, Steve <ghost...@gmail.com> wrote:
Hi Ray,

Thanks for taking the time to look at this. The grey level range is what I get from my data too. The data is what is left over from reconstructing a wavefront after the data is collected from a CCD. For now I display everything in greyscale but I wanted to get the color going since it looked decent when I used imshow. The problem I ran into is exactly what you're running into, which is what do the values represent? 

On Fri, Jun 18, 2010 at 7:26 PM, Ray Pasco <pas...@verizon.net> wrote:
Steve,

I've been playing around withe the script snippet and the data file you provided. My basic problem is that I don't know just what is a "floating point" pixel value !  There seems to be no documentation available anywhere. It appears to be whatever the programmer wants it to mean.

 As mentioned, the values are residuals from some camera data calculations so I need to speak with the astronomers to find out exactly how they should be represented.


For example, what does a pixel value of "-31.3153" mean ? I amd assuming that a value of "0.0" means that this pixel is transparent.  See file [ FP_data_text.txt ]. It forms a perfect 64x64 disc. Is this an output from some kind of special-purpose scanner array ?

The data should be 66 x 66 since I create the array originally and put 0's in certain locations to form the circle image.
 

Consider the program line:

    pilImage = Image.frombuffer ( 'RGBA', (66, 66), d1, 'raw', 'RGBA', 0, 1 )

The first arg says that you want to create an RGBA mode image. Any valid Pil mode may be used here.  The 5th parameter ( 'RGBA' ) tells Pil that the raw data order in the buffer is r-g-b-a, r-g-b-a, ...
How can this be right ?  The buffer consists of only 64x64 pixels which have a single floating point value encoding each pixel. How does one encode an RGBA pixel into a +/- floating point value ?

This is the source of my confusion. I was hoping there was some magical algorithm (or library) that I didn't know about that would take the pixel values (floats) and use bin them to create some image based off of max / min. 
 

Anyway, I scaled the +/- data to the [0 .. 255] grey level range and made 0.0-valued pixels transparent. In reality I have no idea what the floating point values really mean. This is what I got:

Magnified 64x64 image

The patterns suggest that I am on the right track in decoding the FP values. Only the creator of the values in file [ data_float.npy ] knows what the values mean, that is, how to transform them into (back into ?) 4 RGBA-ordered image integer values.



This is what I get too and what the astronomers expect to see so the data is correct. Now just to find a way to get it in color. :)

Thanks again for your help.

- Steve 
ZZ_GREYLEVEL_TRANSPARENCY.PNG

GadgetSteve

unread,
Jun 19, 2010, 3:43:39 AM6/19/10
to wxpytho...@googlegroups.com
If the data is from a sensor such as a radio telescope then what you probably really need is 'false' colour - i.e. some sort of mapping, possibly customizable, that allows differences to be highlighted... I have seen this done with a variety of sensors, e.g. sonar.
Gadget/Steve
 

Thanks again for your help.

- Steve 

--
ZZ_GREYLEVEL_TRANSPARENCY.PNG

WinCrazy

unread,
Jun 19, 2010, 12:45:03 PM6/19/10
to wxPython-users
I think you need to work on color later with a pseudo-color algorithm
after the grey level image is confirmed to be correct. Maybe the
greylevel image is what they want (less the pseudocolor) ! There
does appear to be X-Y correlation (patterns) in the image.

Ray


On Jun 19, 3:43 am, "GadgetSteve" <gadgetst...@live.co.uk> wrote:
> From: Steve
> Sent: Saturday, June 19, 2010 7:43 AM
> To: wxpytho...@googlegroups.com
> Subject: Re: [wxPython-users] Array of 32 bit floats --> RGB Image --> wx.BitmapFromBuffer
>
> Sorry, I hit send before I meant to. :)
>
> On Fri, Jun 18, 2010 at 11:38 PM, Steve <ghostra...@gmail.com> wrote:
>
>   Hi Ray,
>
>   Thanks for taking the time to look at this. The grey level range is what I get from my data too. The data is what is left over from reconstructing a wavefront after the data is collected from a CCD. For now I display everything in greyscale but I wanted to get the color going since it looked decent when I used imshow. The problem I ran into is exactly what you're running into, which is what do the values represent?
>
>   On Fri, Jun 18, 2010 at 7:26 PM, Ray Pasco <pas...@verizon.net> wrote:
>
>     Steve,
>
>     I've been playing around withe the script snippet and the data file you provided. My basic problem is that I don't know just what is a "floating point" pixel value !  There seems to be no documentation available anywhere. It appears to be whatever the programmer wants it to mean.
>
>  As mentioned, the values are residuals from some camera data calculations so I need to speak with the astronomers to find out exactly how they should be represented.
>
>     For example, what does a pixel value of "-31.3153" mean ? I amd assuming that a value of "0.0" means that this pixel is transparent.  See file [ FP_data_text.txt ]. It forms a perfect 64x64 disc. Is this an output from some kind of special-purpose scanner array ?
>
> The data should be 66 x 66 since I create the array originally and put 0's in certain locations to form the circle image.
>
>     Consider the program line:
>
>         pilImage = Image.frombuffer ( 'RGBA', (66, 66), d1, 'raw', 'RGBA', 0, 1 )
>
>     The first arg says that you want to create an RGBA mode image. Any valid Pil mode may be used here.  The 5th parameter ( 'RGBA' ) tells Pil that the raw data order in the buffer is r-g-b-a, r-g-b-a, ...
>     How can this be right ?  The buffer consists of only 64x64 pixels which have a single floating point value encoding each pixel. How does one encode an RGBA pixel into a +/- floating point value ?
>
> This is the source of my confusion. I was hoping there was some magical algorithm (or library) that I didn't know about that would take the pixel values (floats) and use bin them to create some image based off of max / min.
>
>     Anyway, I scaled the +/- data to the [0 .. 255] grey level range and made 0.0-valued pixels transparent. In reality I have no idea what the floating point values really mean. This is what I got:
>
>     Magnified 64x64 image
>
>     The patterns suggest that I am on the right track in decoding the FP values. Only the creator of the values in file [ data_float.npy ] knows what the values mean, that is, how to transform them into (back into ?) 4 RGBA-ordered image integer values.
>
> This is what I get too and what the astronomers expect to see so the data is correct. Now just to find a way to get it in color. :)
>
> If the data is from a sensor such as a radio telescope then what you probably really need is 'false' colour - i.e. some sort of mapping, possibly customizable, that allows differences to be highlighted... I have seen this done with a variety of sensors, e.g. sonar.
> Gadget/Steve
>
> Thanks again for your help.
>
> - Steve
>
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visithttp://groups.google.com/group/wxPython-users?hl=en
>
>  ZZ_GREYLEVEL_TRANSPARENCY.PNG
> 4KViewDownload
Reply all
Reply to author
Forward
0 new messages