Removing images from Memory

I currently have a small program that is loading about 1 - 5 images into memory using wx.FileSystem and wx.MemoryFSHandler. I would like to be able to also remove these image from memory without closing the app and opening it again. I won’t post the full code unless asked for it, but here are the important snippets.

In the wx.Frame class init:

wx.FileSystem.AddHandler(wx.MemoryFSHandler())

self.theMemHandler = wx.MemoryFSHandler()

In the function loading the images to memory:

for image in self.image_list:

_image = self.process_image(image)

new_image = wx.EmptyImage(_image.size[0], _image.size[1])

new_image.SetData(_image.convert(‘RGB’).tostring())

final_image = wx.BitmapFromImage(new_image)

self.theMemHandler.AddFile(image, final_image, wx.BITMAP_TYPE_BITMAP)

I do see a way of removing each image from memory by iterating over my original list again and using self.theMemHandler.RemoveFile(), but I would like to know if there is a better way.

Thanks,

  • Mike S.

Mike Stover wrote:

I currently have a small program that is loading about 1 - 5 images into
memory using wx.FileSystem and wx.MemoryFSHandler. I would like to be
able to also remove these image from memory without closing the app and
opening it again. I won't post the full code unless asked for it, but
here are the important snippets.

In the wx.Frame class __init__:
*wx.FileSystem.AddHandler(wx.MemoryFSHandler())*
*self.theMemHandler = wx.MemoryFSHandler()*
*
In the function loading the images to memory:
*for image in self.image_list:*
*_image = self.process_image(image)*
*new_image = wx.EmptyImage(_image.size[0], _image.size[1])*
*new_image.SetData(_image.convert('RGB').tostring())*
*final_image = wx.BitmapFromImage(new_image)*
*self.theMemHandler.AddFile(image, final_image, wx.BITMAP_TYPE_BITMAP)*

I do see a way of removing each image from memory by iterating over my
original list again and using *self.theMemHandler.RemoveFile()*, but I
would like to know if there is a better way.

Not sure if this is what you are looking for but if you know what filenames you used when adding them to the MemoryFSHandler you should be able to use that with RemoveFile to remove just the ones you want. There isn't any simple way to remove them all. BTW, The AddFile methods and RemoveFile are staticmethods, so you don't need an instance to use them.

···

--
Robin Dunn
Software Craftsman

Thank you Robin,

I was using a few tutorials I had found for using the MemoryFSHandler that all created an instance of it first. With your information I should be able to remove / modify some lines of code. As for removing the images from memory I am keeping a list of the file names as they are being loaded. I have also added a “remove” function that iterates over the list and removes each one. The reason is when the next set of images are loaded they may share names and it caused a few issues.

Thanks again,

  • Mike S.
···

On Tuesday, October 7, 2014 3:00:14 AM UTC-4, Robin Dunn wrote:

Mike Stover wrote:

I currently have a small program that is loading about 1 - 5 images into

memory using wx.FileSystem and wx.MemoryFSHandler. I would like to be

able to also remove these image from memory without closing the app and

opening it again. I won’t post the full code unless asked for it, but

here are the important snippets.

In the wx.Frame class init:

wx.FileSystem.AddHandler(wx.MemoryFSHandler())

self.theMemHandler = wx.MemoryFSHandler()

In the function loading the images to memory:

for image in self.image_list:

_image = self.process_image(image)

new_image = wx.EmptyImage(_image.size[0], _image.size[1])

new_image.SetData(_image.convert(‘RGB’).tostring())

final_image = wx.BitmapFromImage(new_image)

self.theMemHandler.AddFile(image, final_image, wx.BITMAP_TYPE_BITMAP)

I do see a way of removing each image from memory by iterating over my

original list again and using self.theMemHandler.RemoveFile(), but I

would like to know if there is a better way.

Not sure if this is what you are looking for but if you know what
filenames you used when adding them to the MemoryFSHandler you should be
able to use that with RemoveFile to remove just the ones you want.
There isn’t any simple way to remove them all. BTW, The AddFile methods
and RemoveFile are staticmethods, so you don’t need an instance to use them.


Robin Dunn

Software Craftsman

http://wxPython.org