You may use StringIO.

from scipy import *
from pylab import *
from PIL import Image

import cStringIO


a = arange(16384).reshape(128,128)

f = cStringIO.StringIO()
imsave(f, a, cmap=cm.summer,vmin=0,vmax=16383, format="png") # you'd
better set the format explicitly.
f.reset()
b = Image.open(f)
f.close()

IHTH,

-JJ


On Tue, Jun 1, 2010 at 5:18 AM, rugspin <piet_par...@web.de> wrote:
>
>
>
> Angus McMorland-2 wrote:
>>
>> On 31 May 2010 23:17, Angus McMorland <amcm...@gmail.com> wrote:
>>
>>> On 31 May 2010 19:49, rugspin <piet_par...@web.de> wrote:
>>>
>>>>
>>>> I have a small problem how to convert an image from matplotlib to PIL
>>>>
>>>> right now doing somthing like this:
>>>> ------------------------------------------
>>>> from scipy import *
>>>> from pylab import *
>>>> from PIL import Image
>>>>
>>>> a = arange(16384).reshape(128,128)
>>>> imsave( "test.png", a, cmap=cm.summer,vmin=0,vmax=16383)
>>>> b = Image.open("test.png" )
>>>> ------------------------------------------
>>>>
>>>
>>> The Image.fromarray function should do what you want. For example,
>>>
>>> import numpy as np   # note: use of "from foo import *"
>>> import Image             # is discouraged where possible
>>>
>>> a = np.arange(128)[None,:] * np.ones(128)[:,None]
>>>
>>
>> Sorry - I was playing around with a few iterations of this line, and
>> didn't
>> provide the most useful one. Your example:
>>
>> a = np.arange(128**2).reshape(128,128)
>>
>> should also work fine.
>>
>>
>>> b = Image.fromarray(a)
>>> c = np.asarray(b)
>>> np.all(c == a)
>>>   -> True
>>>
>>> I hope that helps,
>>>
>>> Angus.
>>>
>>
>> --
>> AJC McMorland
>> Post-doctoral research fellow
>> Neurobiology, University of Pittsburgh
>>
>> ------------------------------------------------------------------------------
>>
>>
>> _______________________________________________
>> Matplotlib-users mailing list
>> Matplotlib-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>
>>
>
> Thank you Angus
> but that's not exactly what i was looking for. The fromarray function is
> very basic, so I would have to take care of all the RGBA of the array. the
> imshow and imsave functions take care of all that for example adding a
> colormap. After choosing a reasonable colormap (vmin, vmax, ....) I would
> like to convert this into a PIL image.
>
> Best Regard
> Hans
>
> --
> View this message in context: 
> http://old.nabble.com/imshow%2C-imsave-to-PIL-image-conversion-tp28736246p28739401.html
> Sent from the matplotlib - users mailing list archive at Nabble.com.
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>

------------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to