Jesper Larsen wrote:
> Hi mpl users,
>
> I am trying to save a figure to a file like object (a StringIO object)
> and load this object into PIL (Python Imaging Library). The code for
> this is really simple (fig is my figure object):
>
> # This works
> fig.savefig('test.png', format='png')
> im = Image.open('test.png')
>
> # This fails
> imgdata = StringIO.StringIO()
> fig.savefig(imgdata, format='png')
> im = Image.open(imgdata)
>
>   File "/home/jl/testfile.py", line 551, in contour
>     im = Image.open(imgdata)
>   File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1916, in open
>     raise IOError("cannot identify image file")
> IOError: cannot identify image file
>   
You need to "rewind" the StringIO cursor before opening with PIL:

imgdata = StringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0)
im = Image.open(imgdata)


Hope that helps,
Mike

-- 
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to