amitchone
amitchone

Reputation: 1638

Overwrite image in PIL instead of opening new window

I'm sending a stream of JPEG images from a Raspberry Pi to my MBP via a simple socket programme in Python 2.7.

When I read the image from the stream on my MBP, it opens up in Preview and opens a new Preview window for every separate image. I have an fps of about 2/3 and obviously 2/3 new windows per second is impossible to work with.

How can I go about only opening one Preview window and simply overwriting the displayed image? Would OpenCV be the best way to go? If so I am unsure how to.

Here is how I read the stream and display the images:

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()

Upvotes: 0

Views: 1570

Answers (1)

James
James

Reputation: 2663

OS X Preview seems to automatically reload open images at intervals (always when the window receives focus), but Image.show saves a new temporary file each time you use it. I suggest saving each new frame to the same file and then using subprocess.call with the OS X open command.

This being said, 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.

Upvotes: 1

Related Questions