SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: rugspin <pie...@we...> - 2010年05月31日 23:49:35
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" )
------------------------------------------
so I have a 128x128 array, get a 128x128 size png by making use of a
colormap and get a 128x128 size PIL image. But so far I could figure out a
way to do this directly without writing a temporary png and reading it
again, which is quite slow. My important point here is to keep the the pixel
resolution.
I would be glad about some help
Regards Hans
-- 
View this message in context: http://old.nabble.com/imshow%2C-imsave-to-PIL-image-conversion-tp28736246p28736246.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
From: Angus M. <am...@gm...> - 2010年06月01日 03:17:53
On 31 May 2010 19:49, rugspin <pie...@we...> 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]
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
From: Angus M. <am...@gm...> - 2010年06月01日 03:21:34
On 31 May 2010 23:17, Angus McMorland <am...@gm...> wrote:
> On 31 May 2010 19:49, rugspin <pie...@we...> 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
From: rugspin <pie...@we...> - 2010年06月01日 09:18:47
Angus McMorland-2 wrote:
> 
> On 31 May 2010 23:17, Angus McMorland <am...@gm...> wrote:
> 
>> On 31 May 2010 19:49, rugspin <pie...@we...> 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
> Mat...@li...
> 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.
From: Jae-Joon L. <lee...@gm...> - 2010年06月01日 16:34:26
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 <pie...@we...> wrote:
>
>
>
> Angus McMorland-2 wrote:
>>
>> On 31 May 2010 23:17, Angus McMorland <am...@gm...> wrote:
>>
>>> On 31 May 2010 19:49, rugspin <pie...@we...> 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
>> Mat...@li...
>> 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
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
From: rugspin <pie...@we...> - 2010年06月01日 18:22:58
Thanks Jae-Joo
That's doing it, thought there might be an internal way within matplotlib,
but I could figure one.
I'm quite confused with the canvas, dpi, ...
Regards Hans
Jae-Joon Lee wrote:
> 
> 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 <pie...@we...> wrote:
>>
>>
>>
>> Angus McMorland-2 wrote:
>>>
>>> On 31 May 2010 23:17, Angus McMorland <am...@gm...> wrote:
>>>
>>>> On 31 May 2010 19:49, rugspin <pie...@we...> 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
>>> Mat...@li...
>>> 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
>> Mat...@li...
>> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>>
> 
> ------------------------------------------------------------------------------
> 
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> 
> 
-- 
View this message in context: http://old.nabble.com/imshow%2C-imsave-to-PIL-image-conversion-tp28736246p28745632.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
From: Friedrich R. <fri...@gm...> - 2010年06月01日 19:35:07
Attachments: canvas.py
> That's doing it, thought there might be an internal way within matplotlib,
> but I could figure one.
I think you can use the agg backend and the corresponding Canvas'es method
canvas.tostring_rgb()
. Then you can load this in PIL by Image.fromstring.
Well, I simply send you my PIL backend. Tell me if it's still working ...
Friedrich
From: Jae-Joon L. <lee...@gm...> - 2010年06月01日 22:39:32
On Tue, Jun 1, 2010 at 2:22 PM, rugspin <pie...@we...> wrote:
> That's doing it, thought there might be an internal way within matplotlib,
> but I could figure one.
If you need more hard-core way of doing this,
a = np.arange(16384).reshape(128, 128)
from matplotlib.colors import Normalize
import matplotlib.cm as cm
norm = Normalize(vmin=0, vmax=16383)
cmap = cm.summer
A = (cmap(norm(a))*255).astype(np.uint8)
import Image
pilim = Image.fromarray(A, "RGBA")
From: rugspin <pie...@we...> - 2010年06月02日 12:33:56
Thanks Jae-Joon and Friedrich
I will try out both solutions.
Regards Hans
Jae-Joon Lee wrote:
> 
> On Tue, Jun 1, 2010 at 2:22 PM, rugspin <pie...@we...> wrote:
>> That's doing it, thought there might be an internal way within
>> matplotlib,
>> but I could figure one.
> 
> If you need more hard-core way of doing this,
> 
> a = np.arange(16384).reshape(128, 128)
> 
> from matplotlib.colors import Normalize
> import matplotlib.cm as cm
> norm = Normalize(vmin=0, vmax=16383)
> cmap = cm.summer
> 
> A = (cmap(norm(a))*255).astype(np.uint8)
> 
> import Image
> pilim = Image.fromarray(A, "RGBA")
> 
> ------------------------------------------------------------------------------
> 
> _______________________________________________
> Matplotlib-users mailing list
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
> 
> 
-- 
View this message in context: http://old.nabble.com/imshow%2C-imsave-to-PIL-image-conversion-tp28736246p28753655.html
Sent from the matplotlib - users mailing list archive at Nabble.com.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /