在 PIL 中覆盖图像而不是打开新图像 window

Overwrite image in PIL instead of opening new window

我在 Python 2.7 中通过一个简单的套接字程序将 JPEG 图像流从 Raspberry Pi 发送到我的 MBP。

当我从 MBP 上的流中读取图像时,它会在预览中打开并为每个单独的图像打开一个新的预览 window。我有一个大约 2/3 的 fps,显然每秒 2/3 new windows 是不可能的。

我怎样才能只打开一个预览 window 并简单地覆盖显示的图像? OpenCV 会是最好的方法吗?如果是这样,我不确定如何。

以下是我读取流和显示图像的方式:

image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
    if not image_len:
        break
    image_stream = io.BytesIO()
    image_stream.write(connection.read(image_len))
    image_stream.seek(0)
    image = Image.open(image_stream)
    image.show()

OS X 预览似乎会每隔一段时间自动重新加载打开的图像(总是在 window 获得焦点时),但 Image.show 会在您每次使用它时保存一个新的临时文件。我建议将每个新帧保存到同一个文件,然后使用 subprocess.call 和 OS X open 命令。

话虽这么说,the documentation notes that Image.show is primarily for debugging purposes. For a video with more than a few FPS, you probably want something else. One solution would be an HTML interface with WebSockets, perhaps using something like AutoBahn