I want to show an image recreated from an img-vector, everything fine.
now I edit the Vector and want to show the new image, and that multiple times per second.
My actual code open tons of windows, with the new picture in it.
loop
{rearr0 = generateNewImageVector()reimg0 = Image.fromarray(rearr0, 'RGB')reimg0.show()
}
What can I do to create just one Window and always show just the new image?
Another way of doing this is to take advantage of OpenCV's imshow()
function which will display a numpy
image in a window and redraw it each time you update the data.
Note that OpenCV is quite a beast of an installation, but maybe you use it already. So, the code is miles simpler than my pygame
-based answer, but the installation of OpenCV could take many hours/days...
#!/usr/local/bin/python3
import numpy as np
import cv2def sin2d(x,y):"""2-d sine function to plot"""return np.sin(x) + np.cos(y)def getFrame():"""Generate next frame of simulation as numpy array"""# Create data on first call onlyif getFrame.z is None:xx, yy = np.meshgrid(np.linspace(0,2*np.pi,w), np.linspace(0,2*np.pi,h))getFrame.z = sin2d(xx, yy)getFrame.z = cv2.normalize(getFrame.z,None,alpha=0,beta=1,norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)# Just roll data for subsequent callsgetFrame.z = np.roll(getFrame.z,(1,2),(0,1))return getFrame.z# Frame size
w, h = 640, 480getFrame.z = Nonewhile True:# Get a numpy array to display from the simulationnpimage=getFrame()cv2.imshow('image',npimage)cv2.waitKey(1)
That looks like this:
It is dead smooth and has no "banding" effects in real life, but there is a 2MB limit on StackOverflow, so I had to decrease the quality and frame rate to keep the size down.