Convert numpy.ndarray into "normal" array

J

Johannes Bauer

Hi group,

I'm confused, kind of. The application I'm writing currently reads data
from a FITS file and should display it on a gtk window. So far I have:

[...]
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
pb_pixels = pb.get_pixels_array()

print(type(fits_pixels))
print(type(pb_pixels))

which gives

<type 'numpy.ndarray'>
<type 'array'>

So now I want to copy the fits_pixels -> pb_pixels. Doing

pb_pixels = fits_pixels

works and is insanely fast, however the picture looks all screwed-up
(looks like a RGB picture of unititialized memory, huge chunks of 0s
interleaved with lots of white noise).

Doing the loop:

for x in range(width):
for y in range(height):
pb_pixels[y, x] = fits_pixels[y, x]

works as expected, but is horribly slow (around 3 seconds for a 640x480
picture).

So now I've been trying to somehow convert the array in a fast manner,
but just couldn't do it. What exactly is "array" anyways? I know
"array.array", but that's something completely different, right? Does
anyone have hints on how to go do this?

Kind regards,
Johannes
 
M

MRAB

Johannes said:
Hi group,

I'm confused, kind of. The application I'm writing currently reads data
from a FITS file and should display it on a gtk window. So far I have:

[...]
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
pb_pixels = pb.get_pixels_array()

print(type(fits_pixels))
print(type(pb_pixels))

which gives

<type 'numpy.ndarray'>
<type 'array'>

So now I want to copy the fits_pixels -> pb_pixels. Doing

pb_pixels = fits_pixels
This simply makes pb_pixels refer to the same object as fits_pixels. It
doesn't copy the values into the existing pb_pixels object.
works and is insanely fast, however the picture looks all screwed-up
(looks like a RGB picture of unititialized memory, huge chunks of 0s
interleaved with lots of white noise).

Doing the loop:

for x in range(width):
for y in range(height):
pb_pixels[y, x] = fits_pixels[y, x]

works as expected, but is horribly slow (around 3 seconds for a 640x480
picture).
This does copy the values into the existing pb_pixels object.
So now I've been trying to somehow convert the array in a fast manner,
but just couldn't do it. What exactly is "array" anyways? I know
"array.array", but that's something completely different, right? Does
anyone have hints on how to go do this?
You should be able to copy the values using array slicing, something
like:

pb_pixels[:] = fits_pixels

or:

pb_pixels[:, :] = fits_pixels

perhaps.
 
J

Johannes Bauer

MRAB said:
This simply makes pb_pixels refer to the same object as fits_pixels. It
doesn't copy the values into the existing pb_pixels object.

Oh okay, I was thinking of C++ std::vector<char> which behaves
differently :-/ Switching between languages back and forth is not
something I'm good in, appearently.
You should be able to copy the values using array slicing, something
like:

pb_pixels[:] = fits_pixels

or:

pb_pixels[:, :] = fits_pixels

OK, I hadn't been familiar with that syntax at all - need to catch up
reading on this.

However, it doesn't work (altough probably for another reason):

Traceback (most recent call last):
File "./guiclient.py", line 433, in on_Result
self.__updateresult()
File "./guiclient.py", line 385, in __updateresult
pb_pixels[:, :] = fits_pixels
TypeError: Array can not be safely cast to required type

I guess Python fears truncation. However I ensure in advance the values
won't be truncated. How can I convince Python that it may safely assume
that is true?

Kind regards,
Johannes
 
A

Aahz

So now I want to copy the fits_pixels -> pb_pixels. Doing

pb_pixels = fits_pixels

works and is insanely fast, however the picture looks all screwed-up
(looks like a RGB picture of unititialized memory, huge chunks of 0s
interleaved with lots of white noise).

Doing the loop:

for x in range(width):
for y in range(height):
pb_pixels[y, x] = fits_pixels[y, x]

works as expected, but is horribly slow (around 3 seconds for a 640x480
picture).

So now I've been trying to somehow convert the array in a fast manner,
but just couldn't do it. What exactly is "array" anyways? I know
"array.array", but that's something completely different, right? Does
anyone have hints on how to go do this?

http://scipy.org/Mailing_Lists
 
P

Piet van Oostrum

Johannes Bauer said:
JB> Hi group,
JB> I'm confused, kind of. The application I'm writing currently reads data
JB> from a FITS file and should display it on a gtk window. So far I have:
JB> [...]
JB> pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
JB> pb_pixels = pb.get_pixels_array()
JB> print(type(fits_pixels))
JB> print(type(pb_pixels))
JB> which gives
JB> <type 'numpy.ndarray'>
JB> <type 'array'> [...]
JB> So now I've been trying to somehow convert the array in a fast manner,
JB> but just couldn't do it. What exactly is "array" anyways? I know
JB> "array.array", but that's something completely different, right? Does
JB> anyone have hints on how to go do this?

I think it is array from Numeric, because the releases of pygtk are
still built with Numeric instead of numpy. In SVN it has been converted
to use Numpy instead, so then type(pb_pixels) would also be
'numpy.ndarray' and no conversion would be necessary.

So try to install the version from SVN.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top