[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Problem with pygame.image.load() and a gzip.open() stream



Paul Broadhead wrote:
Marius Gedminas wrote:

Why do you want a gzipped bmp file rather than a png?

My application is a utility for a game, the game currently uses straight bmp files but will supply them gzipped in the future. The file type is out of my control.

Which file type is out of your control, the .bmp image format or the gzip compression. I tried loading an image from a gzip.GzipFile object, a cStringIO.StringIO object and a bz2.BZ2File object. Only the GzipFile object failed. I could find only one significant difference between a GzipFile and the other two, GzipFile is a classic class while the other two are new-style classes (extension types).

This leaves four possibilities:

1) Use bz2 compression.
2) Read the gzip file into a string and use StringIO to pass the string to pygame.image.load().
3) Create a new-style wrapper class for gzip.GzipFile
4) Create your own new-style version of GzipFile


This wrapper class works for option (3):

class MyFile(object):
   def __init__(self, f):
       self.f = f
   def read(self, n):
       return self.f.read(n)
   def seek(self, offset, whence=0):
       self.f.seek(offset, whence)
   def tell(self):
       return self.f.tell()

where f is any file-like object.

--
Lenard Lindstrom
<len-l@xxxxxxxxx>