Animation with PIL images on wxPython

I am the author of a so-called discrete event simulation package called salabim, that is also able to do real-time animations. See for instance https://www.youtube.com/watch?v=ibQrZ3B76Fo
At this moment that’s done in tkinter (where many layered PIL images are displayed). These PIL images change position, angle, size, etc in time and disappear/appear over time.

I use only PIL images on a canvas and just one or two UI widgets.

I have been looking at the documentation but can’t find out whether this could be realized with wxPython.

Any advice is useful.

For more information on salabim, see www.salabim.org.

I think you should be able to do this by creating a wx.Panel and binding
wx.EVT_PAINT to a method that does (approximately):

class ImagePanel(wx.Panel):
    def __init__(self, parent, size=(800, 600)):
        super(ImagePanel, self).__init__(parent, -1, size=size)
        self.Bind.(wx.EVT_PAINT, self.onPaint)

    def get_PIL_Image(self):
        """overwrite to return PIL Image"""
        pass

    def onPaint(self, event=None):
        pil_image = self.get_PIL_Image()
        width, height = pil_image.size
        bitmap = wx.Bitmap(pil_image.tobytes(), width, height)

        # paint the bitmap
        painter = wx.AutoBufferedPaintDC(self)
        painter.Clear()
        painter.DrawBitmap(bitmap)

There are other things you might want to do like scale the bitmap to better
fit in the Panel and/or add padding, add resizing events, and so on. But
that should be the basics of "draw PIL Image to wx Panel".

Hope that helps,

--Matt

···

On Thu, Mar 29, 2018 at 11:18 AM, Ruud van der Ham <rt.van.der.ham@gmail.com > wrote:

I am the author of a so-called discrete event simulation package called
salabim, that is also able to do real-time animations. See for instance
https://www.youtube.com/watch?v=ibQrZ3B76Fo
At this moment that's done in tkinter (where many layered PIL images are
displayed). These PIL images change position, angle, size, etc in time and
disappear/appear over time.
I use only PIL images on a canvas and just one or two UI widgets.

I have been looking at the documentation but can't find out whether this
could be realized with wxPython.

Any advice is useful.

For more information on salabim, see www.salabim.org.

This page on the wiki should be helpful:

https://wiki.wxpython.org/WorkingWithImages

Note that has probably not been updated for wxPython4. But it should be similar, and updates would be great if you do find places that are out of date.

There are also a few examples here that involve image rendering:

https://github.com/PythonCHB/wxPythonDemos

Also not all updated for 4.* -/ PRs gladly accepted.

Also note that wxImage may provide what you need without PIL -/ it is not as full featured, but it does have the basics.

-CHB

-CHB

···

On Mar 29, 2018, at 11:12 AM, Matt Newville newville@cars.uchicago.edu wrote:

On Thu, Mar 29, 2018 at 11:18 AM, Ruud van der Ham rt.van.der.ham@gmail.com wrote:

I am the author of a so-called discrete event simulation package called salabim, that is also able to do real-time animations. See for instance https://www.youtube.com/watch?v=ibQrZ3B76Fo
At this moment that’s done in tkinter (where many layered PIL images are displayed). These PIL images change position, angle, size, etc in time and disappear/appear over time.

I use only PIL images on a canvas and just one or two UI widgets.

I have been looking at the documentation but can’t find out whether this could be realized with wxPython.

Any advice is useful.

For more information on salabim, see www.salabim.org.

I think you should be able to do this by creating a wx.Panel and binding wx.EVT_PAINT to a method that does (approximately):

class ImagePanel(wx.Panel):

def init(self, parent, size=(800, 600)):

super(ImagePanel, self).init(parent, -1, size=size)

self.Bind.(wx.EVT_PAINT, self.onPaint)

def get_PIL_Image(self):

“”“overwrite to return PIL Image”“”

pass

def onPaint(self, event=None):

pil_image = self.get_PIL_Image()
width, height = pil_image.size

bitmap = wx.Bitmap(pil_image.tobytes(), width, height)

    # paint the bitmap

painter = wx.AutoBufferedPaintDC(self)

painter.Clear()

painter.DrawBitmap(bitmap)

There are other things you might want to do like scale the bitmap to better fit in the Panel and/or add padding, add resizing events, and so on. But that should be the basics of “draw PIL Image to wx Panel”.

Hope that helps,

–Matt

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.