我想展示一个从img-vector重新创建的图像,一切都很顺利。 现在我正在编辑矢量图,并希望每秒钟显示新图像多次。 我的实际代码打开了大量窗口,其中包含新图片。
loop
{
rearr0 = generateNewImageVector()
reimg0 = Image.fromarray(rearr0, 'RGB')
reimg0.show()
}
我要怎样才能创建一个窗口,并始终显示最新的图像?
另一种方法是利用 OpenCV 的 imshow()
函数,在窗口中显示一个 numpy
图像,并在更新数据时重新绘制它。
请注意,OpenCV 的安装相当复杂,但也许您已经在使用它了。所以,这段代码比基于pygame
的答案简单得多,但是安装OpenCV可能需要很多小时/天......
#!/usr/local/bin/python3
import numpy as np
import cv2
def 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 only
if 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 calls
getFrame.z = np.roll(getFrame.z,(1,2),(0,1))
return getFrame.z
# Frame size
w, h = 640, 480
getFrame.z = None
while True:
# Get a numpy array to display from the simulation
npimage=getFrame()
cv2.imshow('image',npimage)
cv2.waitKey(1)
看起来是这样的:
在现实生活中,它非常流畅,没有"条纹"效应,但在StackOverflow上有 2MB 的限制,所以我不得不降低质量和帧率以保持尺寸。
pip3 install --user opencv-python
即可。如果您的操作系统比较旧,可以尝试运行pip3 install --user opencv-python==3.3.0.10
。 - N. Virgo# 窗口大小
w, h = (800, 640)
- Ismael Harunpygame
相当简单快速地完成这项任务。getFrame()
的函数,该函数返回一个numpy
数组,其中包含由模拟计算得出的图像。#!/usr/local/bin/python3
import numpy as np
import pygame
h,w=480,640
border=50
N=0
getFrame.z = None
def 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 only
if getFrame.z is None:
xx, yy = np.meshgrid(np.linspace(0,2*np.pi,h), np.linspace(0,2*np.pi,w))
getFrame.z = sin2d(xx, yy)
getFrame.z = 255*getFrame.z/getFrame.z.max()
# Just roll data for subsequent calls
getFrame.z = np.roll(getFrame.z,(1,2),(0,1))
return getFrame.z
pygame.init()
screen = pygame.display.set_mode((w+(2*border), h+(2*border)))
pygame.display.set_caption("Serious Work - not games")
done = False
clock = pygame.time.Clock()
# Get a font for rendering the frame number
basicfont = pygame.font.SysFont(None, 32)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Clear screen to white before drawing
screen.fill((255, 255, 255))
# Get a numpy array to display from the simulation
npimage=getFrame()
# Convert to a surface and splat onto screen offset by border width and height
surface = pygame.surfarray.make_surface(npimage)
screen.blit(surface, (border, border))
# Display and update frame counter
text = basicfont.render('Frame: ' + str(N), True, (255, 0, 0), (255, 255, 255))
screen.blit(text, (border,h+border))
N = N + 1
pygame.display.flip()
clock.tick(60)
这看起来像这样。在现实生活中,它非常快速和流畅,但是StackOverflow上的视频大小限制为2MB,因此我生成了一个帧率较低且尺寸较小的GIF,只是为了将其保持在2MB以下。
显然,您可以添加检测向上和向下箭头以加快或减慢动画速度,您还可以检测左/右箭头和空格键来向后/向前或暂停动画。Image.show()
主要用于调试目的。在Windows上,它将图像保存到临时BMP文件中,并使用标准BMP显示实用程序来显示它。import matplotlib.pyplot as plt
imgplot = plt.imshow(rearr0) # "plot" the image.
对于每次迭代,您可以调用imgplot.clf()
函数,它将清除绘图但保留轴。我尚未尝试过,但应该可以使用。