[wxpython-users] How to display image from a list

Robert Robert robertrobert93 at yahoo.com
Thu May 1 00:02:02 PDT 2008


In a quick example I've tried what you said but with the 
   
  import numpy as np
  pixValueList = [0,1,2,3,4,5,6,7,8,9]
  width = 2
  height = 5
  RGB = np.zeros((width, height, 3), dtype=np.uint8)
  RGB[:,:,0] = pixValueList
   
  I get the error

  ValueError: shape mismatch: objects cannot be broadcast to a single shape
   
  How do I fill the RGB array correctly. Note that len( pixValueList ) = width * height
   
  
Christopher Barker <Chris.Barker at noaa.gov> wrote:
  Robert Robert wrote:
> I have a monochromatic grayscale image. So 
> it is not a list of tuples or r,g, b values. So actually how do I set 
> the data to a wx.Image. So a simple example. Let say I have an image of 
> size , width 2, height 5. and the pixel values are stored as integer in 
> a list
> 
> pixValueList = [0,1,2,3,4,5,6,7,8,9]

First, it's a bit odd, and inefficient, to have this in a list, you'd be 
better of with the data in a string or array.array.

> how do I display this image ?

I'm pretty sure wx only support RBG images to be loaded up in this way. 
So, you'll need to use another tool to convert your data to RGB. Two 
options are:

1) PIL: The Python Imaging library

It should be able to do this for you, and once you have an RBG image in 
PIL, you can make a wxImage out of it:

http://wiki.wxpython.org/WorkingWithImages

2) numpy: Numerical python
numpy provides a n-dimensional array object, and lot of tools for doing 
math with them. In this case, an RBG image is a Width X Height X 3 
array. So for the above data:

import numpy as np

# create an array for your data
RGB = np.zeros((width, height, 3), dtype=np.uint8)
# fill it in: (R = G = B fr grey)
RGB[:,:,0] = pixValueList
RGB[:,:,1] = pixValueList
RGB[:,:,2] = pixValueList
# scale it to 0-255:
RGB *= 255.0/MaxValue # MaxValue is the value of white in your data

#now you've got RBG image data. To make a wx.Image:
image = wx.EmptyImage(width,height)
image.SetData( RGB.tostring())

-Chris









> Thanks in advance.
> 
> robert
> 
> ------------------------------------------------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try 
> it now. 
> > >
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> wxpython-users mailing list
> wxpython-users at lists.wxwidgets.org
> http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

-- 
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.Barker at noaa.gov
_______________________________________________
wxpython-users mailing list
wxpython-users at lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile.  Try it now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/20080501/a2dc3893/attachment.htm


More information about the wxpython-users mailing list