I am writing an image processing module in Python using matplotlib.pyplot and numpy backend. The images will largely be in tiff format, so the code below uses tifffile to convert a 3D image file to a 4D array in numpy. The below code aims to move through the z-plane of the 3D image, one image at a time, using z and x as hotkeys. My problem is quite interesting and I can't figure it out: the time between event and action (pressing x and displaying z+1 image) gets twice as long with each event. I timed it, results below: 1st z-press: 0.124 s 2nd z-prss: 0.250 s 3rd z-press: 0.4875 s
It is a bonafide linear increase, but I can't find where in my code the bug could be.
import matplotlib.pyplot as plt
import numpy as np
import tifffile as tiff
class Image:
def __init__ (self, fname):
self.fname = fname
self.fig = plt.figure()
self.z = 0
self.ax = self.fig.add_subplot(111)
self.npimg = tiff.imread(self.fname)
self.plotimg()
self.connect()
def plotimg(self):
plt.imshow(self.npimg[self.z][0])
plt.show()
def connect(self):
self.cidkeypress = self.fig.canvas.mpl_connect('key_press_event',self.keypress)
def disconnect(self):
self.fig.canvas.mpl_disconnect(self.cidkeypress)
def keypress(self, event):
if event.key == 'x':
self.z += 1
self.plotimg()
elif event.key == 'z':
self.z -= 1
self.plotimg()