load data from string or array to Image

Forgot to reply-to-list...

···

---------- Forwarded message ----------
From: Anthony Floyd <anthonyfloyd@...287...>
Date: Wed, Mar 26, 2008 at 4:54 PM
Subject: Re: [Matplotlib-users] load data from string or array to Image
To: sa6113 <s.payandeh@...287...>

On Wed, Mar 26, 2008 at 1:33 AM, sa6113 <s.payandeh@...287...> wrote:
>
>
> I have a problem to load data from string or array to Image but without
> using PIL , because I have to check the application in 6 different platforms
> like Windows 32bit, Windows X64 (64bit version), Linux 32bit, Linux 64bit
> x86_64, Linux IPF (Itanium Processor Family) and HP-UX 64.
>
> There is a code but I don't want to use PIL :
> http://mail.python.org/pipermail/image-sig/1998-October/000572.html
>
> Can I accomplish the same thing using the modules that are already on the
> system, or those that are pure Python (not requiring any compilation or
> binary download)?

I went through this just a few weeks ago. Now, mind you I'm using wx
and numpy already.

My solution was to turn my .png into a Python string using img2py.py
in the wx/tools directory.

With the string, I then used the following code to return the image (a
watermark) as an array that figimage() or imshow() can use directly.
I'm sure the code can be optimized, but it works fast enough for me...

def getWatermarkArray():
    rows = 28
    columns = 200
    dimensions = 3

    image = getWatermarkImage()
    # image array is a string of hex values
    imageString = image.GetData()

    imageList = [ord(item) for item in imageString]

    imageArray = numpy.zeros(shape=(28,200,3), dtype=numpy.float32)

    imageCounter = 0

    for rowCounter in range(rows):
        for columnCounter in range(columns):
            for dimCounter in range(dimensions):
                imageArray[rowCounter][columnCounter][dimCounter] =
imageList[imageCounter]/255.
                imageCounter += 1

    return imageArray

HTH,
>

I use matplotlib and Backend Agg to draw a plot , I want to show this plot in
my GUI in specific area (Plot area) , I need to have the image object in
oder to show it, so I have to convert this plot to string or array or save
in buffer and then load it to an Image , I want to know , can I accomplish
this without using PIL , because I need to build, test and verify any new
package added to the system on 6 different platforms, and it might take a
long time and at high expertise level to add the module to our entire
system.

What I have to do?
I can convert it to string or array with numpy or FigureCanvas or ect. , my
problem is how to load this array or string to image !
Is it clear?

Anthony Floyd-2 wrote:

···

Forgot to reply-to-list...

---------- Forwarded message ----------
From: Anthony Floyd <anthonyfloyd@...287...>
Date: Wed, Mar 26, 2008 at 4:54 PM
Subject: Re: [Matplotlib-users] load data from string or array to Image
To: sa6113 <s.payandeh@...287...>

On Wed, Mar 26, 2008 at 1:33 AM, sa6113 <s.payandeh@...287...> wrote:
>
>
> I have a problem to load data from string or array to Image but
without
> using PIL , because I have to check the application in 6 different
platforms
> like Windows 32bit, Windows X64 (64bit version), Linux 32bit, Linux
64bit
> x86_64, Linux IPF (Itanium Processor Family) and HP-UX 64.
>
> There is a code but I don't want to use PIL :
> [Image-SIG] Re: Image to MumPy array with PIL
>
> Can I accomplish the same thing using the modules that are already on
the
> system, or those that are pure Python (not requiring any compilation
or
> binary download)?

I went through this just a few weeks ago. Now, mind you I'm using wx
and numpy already.

My solution was to turn my .png into a Python string using img2py.py
in the wx/tools directory.

With the string, I then used the following code to return the image (a
watermark) as an array that figimage() or imshow() can use directly.
I'm sure the code can be optimized, but it works fast enough for me...

def getWatermarkArray():
    rows = 28
    columns = 200
    dimensions = 3

    image = getWatermarkImage()
    # image array is a string of hex values
    imageString = image.GetData()

    imageList = [ord(item) for item in imageString]

    imageArray = numpy.zeros(shape=(28,200,3), dtype=numpy.float32)

    imageCounter = 0

    for rowCounter in range(rows):
        for columnCounter in range(columns):
            for dimCounter in range(dimensions):
                imageArray[rowCounter][columnCounter][dimCounter] =
imageList[imageCounter]/255.
                imageCounter += 1

    return imageArray

HTH,
>

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
View this message in context: http://www.nabble.com/load-data-from-string-or-array-to-Image-tp16297653p16322508.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I use matplotlib and Backend Agg to draw a plot , I want to show this plot in
my GUI in specific area (Plot area) , I need to have the image object in

[snip]

Is it clear?

Not to me :slight_smile:

Do you mean that you've already created a plot and just want to show
it in your GUI?

What GUI toolkit are you using? You may want to look in the various
embedding_in_*.py examples.

···

On Wed, Mar 26, 2008 at 10:38 PM, sa6113 <s.payandeh@...287...> wrote:

It's a very good idea, when posting questions to a list like this, to ask not only the specific question, but also give the background, so we can wee what problem you are trying to solve.

sa6113 wrote:

I use matplotlib and Backend Agg to draw a plot , I want to show this plot in
my GUI in specific area (Plot area) ,

This is background we need. However, you've left out a key point: what GUI toolkit are you using? There is no such thing as a python "image", but each toolkit has it's own implementation, so each needs to be treated differently.

  I need to have the image object in

oder to show it, so I have to convert this plot to string or array or save
in buffer and then load it to an Image ,

If you are using any of the supported back ends: Tk, QT, wx, GTK, then MPL has already solved that problem for you -- see the "embedded_in**" examples.

If you are using something else, then take a look at the various back-end codes -- one of them is probably similar enough to yous to give you inspiration.

And yes, you can get a string or buffer object of RBG data from FigureCanvasAgg.

I want to know , can I accomplish
this without using PIL ,

You shouldn't need PIL.

-Chris

···

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