Encountering a wxPython assertion error with matplotlib when handling a large sequence of images. Below is alternate sample code:
import wx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as CanvasWidget
class ImagePanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.fig = plt.Figure(facecolor='dimgray')
self.canvas = CanvasWidget(self, -1, self.fig)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
self.SetSizer(sizer)
def display_image(self, image):
self.fig.clear()
ax = self.fig.add_subplot(111)
ax.imshow(image, cmap='viridis')
ax.axis('off')
self.canvas.draw()
class MainWindow(wx.Frame):
def __init__(self):
super().__init__(None, title='Process Stack')
self.panel = ImagePanel(self)
self.start_btn = wx.Button(self, label='Start')
self.progress = wx.Gauge(self, range=10)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel, 1, wx.EXPAND)
sizer.Add(self.start_btn, 0, wx.ALL | wx.CENTER, 5)
sizer.Add(self.progress, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(sizer)
self.start_btn.Bind(wx.EVT_BUTTON, self.run_process)
self.Show()
def run_process(self, event):
img_stack = np.random.rand(10, 100, 100)
for idx, img in enumerate(img_stack):
self.panel.display_image(img)
self.progress.SetValue(idx + 1)
if __name__ == '__main__':
app = wx.App(False)
MainWindow()
app.MainLoop()