help for conversion of NUMARRAY to PIL object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A. L.

    #1

    help for conversion of NUMARRAY to PIL object

    hi, everybody here,

    I am a newbie to python. I encounter a problem that how to convert
    an array of numarray to pil object. For example, the data in an image
    is extracted using Image.getdata, then the data are converted into an
    array in numarray. But when the array is needed to convert to the pil
    object, I lose my mind. Could somebody here provide some solutions to
    this problem? (In other words, all I need is the interface between
    reading/writing image data and NUMARRAY.)

    Thanks in advance.

  • Robert Kern

    #2
    Re: help for conversion of NUMARRAY to PIL object

    A. L. wrote:[color=blue]
    > hi, everybody here,
    >
    > I am a newbie to python. I encounter a problem that how to convert
    > an array of numarray to pil object. For example, the data in an image
    > is extracted using Image.getdata, then the data are converted into an
    > array in numarray. But when the array is needed to convert to the pil
    > object, I lose my mind. Could somebody here provide some solutions to
    > this problem? (In other words, all I need is the interface between
    > reading/writing image data and NUMARRAY.)[/color]

    In [11]: from numarray import *

    In [12]: import Image

    In [13]: a = zeros((256,256, 4), UInt8)

    In [14]: img = Image.frombuffe r('RGBA', (256,256), a)

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • A. L.

      #3
      Re: help for conversion of NUMARRAY to PIL object

      Thanks. But some problems remain.

      I have known using Image.tostring/fromstring and
      numarray.tostri ng/fromstring can do the job. But when confronting
      multi-spectral images, e.g., RGB color images, I don't know how to do
      it. Could you give some advices on that?

      You wrote "In [14]: img = Image.frombuffe r('RGBA', (256,256), a) ", but
      it seems that "a" cannot work in "frombuffer ".

      Comment

      • Robert Kern

        #4
        Re: help for conversion of NUMARRAY to PIL object

        A. L. wrote:[color=blue]
        > Thanks. But some problems remain.
        >
        > I have known using Image.tostring/fromstring and
        > numarray.tostri ng/fromstring can do the job. But when confronting
        > multi-spectral images, e.g., RGB color images, I don't know how to do
        > it. Could you give some advices on that?[/color]

        What's not working for you?
        [color=blue]
        > You wrote "In [14]: img = Image.frombuffe r('RGBA', (256,256), a) ", but
        > it seems that "a" cannot work in "frombuffer ".[/color]

        What do you mean that "a" cannot work in "frombuffer "? It certainly does
        on my machine. Is it raising an exception on yours? If so, please post
        the code that is causing the error and the exception that is raised and
        the versions of PIL and numarray that you are using.

        --
        Robert Kern
        rkern@ucsd.edu

        "In the fields of hell where the grass grows high
        Are the graves of dreams allowed to die."
        -- Richard Harter

        Comment

        • A. L.

          #5
          Re: help for conversion of NUMARRAY to PIL object

          Sorry, I make the mistakes. I have known how to use to/fromstring
          method to interface between PIL and Numarray.
          And your code does work.

          Another question. Just like the code you provide, is it possible to
          directly load image data from PIL to Numarray array without use of
          to/fromstring method?

          Thank you very much for your kindly help.

          A.L.

          Comment

          Working...