Hello,
Suppose I have a PIL image and want to convert it to a wx.Bitmap. I
would do this:
img = Image.open(...)
w, h = img.size
s = img.tostring()
bmp = wx.BitmapFromBuffer(w, h, s)
This works great, but is does have a problem. If the image is very big
(say, 100MB), right after the bmp is created, I'll have 300MB
allocated (img, s, bmp) if I need to keep a reference to img. Surely,
s will be soon garbage collected, but what matters is that at sometime
in the execution, there is 300MB allocated.
It would be great if there was something like BitmapFromIterator so I
could do something like this:
img = Image.open(...)
w, h = img.size
pix_access = img.load()
def iter()
for y in xrange(h):
for x in xrange(w):
for i in xrange(3):
yield pix_access[x, y][i]
bmp = wx.BitmapFromIterator(w, h, iter())
This way, only 200MB would be allocated. Also, this would solve other
problems e.g.: when using the FreeImage library, if I need to convert
a image to a wx.Bitmap, I need to swap 'R' and 'G' values because the
library works with 'BGR' order. In order to do this, I need to
allocate a new buffer, copy the bytes and swap them there, so I run
into the same problem of having 300MB allocated. With a iterator I
could solve this.
What do you think? Is there another existing way to solve this? Or is
it a good (or stupid) idea to implement? I don't think it would be
difficult, but I'm not sure how the performance would be. I could try
to implement it myself, but I would need some time since I never tried
to build wxPython myself...
Thanks,
Conrado