Convert numpy.ndarray into "normal" array

Discussion in 'Python' started by Johannes Bauer, Apr 24, 2009.

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

    --
    "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
    verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
    -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
    <48d8bf1d$0$7510$>
     
    Johannes Bauer, Apr 24, 2009
    #1
    1. Advertising

  2. Johannes Bauer

    MRAB Guest

    Johannes Bauer wrote:
    > 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.
     
    MRAB, Apr 24, 2009
    #2
    1. Advertising

  3. MRAB schrieb:

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


    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

    --
    "Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
    verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
    -- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
    <48d8bf1d$0$7510$>
     
    Johannes Bauer, Apr 24, 2009
    #3
  4. Johannes Bauer

    Aahz Guest

    In article <>,
    Johannes Bauer <> wrote:
    >
    >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
    --
    Aahz () <*> http://www.pythoncraft.com/

    "If you think it's expensive to hire a professional to do the job, wait
    until you hire an amateur." --Red Adair
     
    Aahz, Apr 24, 2009
    #4
  5. >>>>> Johannes Bauer <> (JB) wrote:

    >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.
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Apr 25, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TG
    Replies:
    2
    Views:
    657
  2. Marc Oldenhof
    Replies:
    1
    Views:
    946
    sergebb
    Jan 15, 2014
  3. anti-suho
    Replies:
    0
    Views:
    484
    anti-suho
    Feb 25, 2009
  4. Enrico

    Numpy ndarray to C array

    Enrico, Dec 8, 2011, in forum: Python
    Replies:
    1
    Views:
    378
    Robert Kern
    Dec 8, 2011
  5. Radek Machulka

    Shear image (numpy.ndarray)

    Radek Machulka, Apr 27, 2013, in forum: Python
    Replies:
    0
    Views:
    142
    Radek Machulka
    Apr 27, 2013
Loading...

Share This Page