tags:

views:

54

answers:

1

Given a string containing jpeg image data, is it possible to load this directly in pygame?

I've tried using StringIO but failed and I don't completely understand the 'file-like' object concept.

Currently, as a workaround, I'm saving to disk and then loading an image the standard way:

# imagestring contains a jpeg

f=open('test.jpg','wb')
f.write(imagestring)
f.close()
image=pygame.image.load('test.jpg')

Any suggestions on improving this so that we avoid creating a temp file?

+2  A: 
fstr = cStringIO.StringIO(simage)
pygame.image.load(fstr, namehint="somethinguseful")
Ignacio Vazquez-Abrams
I'm getting a "TypeError: load_extended() takes no keyword arguments" error.
zaf
Without the namehint parameter it seems to be working, I'll do some more testing.
zaf
*shrug* I'm just parroting what it says in the docs.
Ignacio Vazquez-Abrams
I must have missed that in the docs. You have the link?
zaf
http://www.pygame.org/docs/ref/image.html#pygame.image.load
Ignacio Vazquez-Abrams
Hmmm, I have seen that. Guess optional means don't try it unless it doesn't work. Bah.
zaf