0

Here's my code to one-time plot a bitmap stored as a numpy array:

bitmapf = np.array((ypixels, xpixels, 3))  # RGB for last channel

# ! bitmapf is populated

renormed255 = 255.0 * bitmapf / np.max(bitmapf)

# https://stackoverflow.com/questions/49271913/convert-numpy-array-to-rgb-image/49334548
from PIL import Image

img = Image.fromarray(renormed255.astype('uint8'), 'RGB')

img.show()

However, I have live data coming in from a websocket populating bitmapf.

EDIT: Live data is sparse, and put through an algorithm to light up pixels on the bitmap. x-axis is time, and only a 1 x pixelsY column is modified for every 20 or so packets recieved.

I wish to render it onscreen at 30Hz.

How to achieve this? (It's big, at least 1024x768).

Is it possible to optimize so only the dirty column is passed to the renderer?

UPDATE: I've tried implementing an opencv approach, but it won't render:

import cv2
import numpy as np
from time import sleep

bitmap = np.zeros((512,512,3),np.uint8)

for i in range(512):
    sleep(.1)
    bitmap[i,i,:] = 128
    cv2.imshow("Color Image", bitmap)

cv2.waitKey(0)
cv2.destroyAllWindows()

xref: Fast Live Plotting in Matplotlib / PyPlot

P i
  • 25,182
  • 33
  • 133
  • 229
  • If you plan to play a video stream in Python by sending raw image on the network, it is *really inefficient*. The required throughput will be `1024 * 768 * 30Hz * 3 channels / 1024^2 = 67.5 MiB/s`. – Jérôme Richard Mar 13 '21 at 16:33
  • Don't bother converting to PIL because PIL's `show()` is a single-shot display. Instead, use **OpenCV** `cv2.imshow()` followed by `cv2.waitKey()`and you can pass it an unchanged Numpy array too. Consider JPEG-compressing before and after transmission to save bandwidth. Or go to YUV with chroma sub-sampling to halve the bandwidth required. – Mark Setchell Mar 13 '21 at 16:40
  • My apologies, I have updated the question with relevant information. Thankyou for insightful comments! – P i Mar 13 '21 at 17:33
  • OpenCV can generally cope with 30 full frames a second anyway, so just splice in your changes and let it render the whole frame. – Mark Setchell Mar 13 '21 at 21:02
  • Remove `sleep()` and shift `cv2.waitkey(30)` 4 spaces to the right so it is inside the loop. – Mark Setchell Mar 14 '21 at 08:13
  • Are you up and running now? – Mark Setchell Mar 15 '21 at 08:33
  • Yes, thanks! I'll provide an answer. – P i Mar 15 '21 at 08:47
  • Working example at: https://stackoverflow.com/questions/66623228/can-i-render-an-opencv-animation-from-a-background-thread-in-python – P i Mar 15 '21 at 08:47

0 Answers0