Animation with PIL images on wxPython

117 views
Skip to first unread message

Ruud van der Ham

unread,
Mar 29, 2018, 12:26:27 PM3/29/18
to wxPython-users
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.


 

Matt Newville

unread,
Mar 29, 2018, 2:13:30 PM3/29/18
to wxpytho...@googlegroups.com
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


Chris Barker - NOAA Federal

unread,
Aug 14, 2018, 11:30:40 AM8/14/18
to wxpytho...@googlegroups.com
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:


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

Sent from my iPhone
--
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-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages