title: Stream continuous data from a python script to NodeJS
body: <p>I have a multi-threaded python script that converts an image to a NumPy array (occupancy grid). The python script then continuously changes the values of the array to simulate a dot moving in a map. This array is converted back to an image using Pillow and then encoded to base64. The NodeJS part has an express server running that also has a SocketIO connection with a mobile app.</p>
  <p><strong>What I am trying to do is:</strong>
  send the encoded image to the mobile app from the server, so the option that came to mind was to run the python script from nodeJs, and as the python script transmits an encoded image the server will redirect it to the mobile app.</p>
  <p><strong>What I'm looking for is:</strong></p>
  <ul>
  <li>stream the python script output to nodeJS without stopping (until the simulation stops)</li>
  <li>or use a better-purposed solution to get the data from the python to the mobile phone</li>
  </ul>
  <p>Thanks in advance!</p>
  <p>python script to run:</p>
  <pre><code>import matplotlib.pyplot as plt
  import cv2
  import threading
  import base64
  from PIL import Image
  temp = 0
  global im_bw
  pixel = 0
  
  def update_image():
      global im_bw
      im2 = Image.fromarray(im_bw)
      image64 = base64.b64encode(im2.tobytes())
      # print the base64 encoded image to javascript
      print(image64)
      threading.Timer(1, update_image).start()
  
  print(&quot;initialization&quot;)
  im_gray = cv2.imread(r'gridMapBlue.JPG', cv2.IMREAD_GRAYSCALE)
  (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  thresh = 127
  im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
  
  
  
  # noinspection PyBroadException
  try:
      update_image()
  except Exception as e:
      print(e.with_traceback())
      pass
  
  # simulate the dot moving by modifying the 
  while True:
      if pixel == 200:
          break
      temp = im_bw[150,pixel]
      im_bw[150, pixel] = 127
      pixel += 1
  
  
  </code></pre>
  <p>Javascript code:</p>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  const io = require(&quot;./api/services/SocketIoService&quot;)
  const spawn = require('child_process').spawn;
  
  
  const socketIO = new io(app);
  socketIO.server.listen(6969, () =&gt; console.log(&quot;server started&quot;))
  
  
  py = spawn('python',[&quot;E:\\QU\\Senior\\robot_positioning\\robot_position_map.py&quot;])
  
  py.stdout.on('data', (data)=&gt;console.log(data));
  
  py.stderr.on(&quot;error&quot;, err =&gt;console.log(err))
  
  </code></pre>
  <p><a href="https://imgur.com/a/orO9FM5" rel="nofollow noreferrer">image used</a></p>
  <p><strong>edit</strong>: added the code for creating the occupancy grid and the javascript code that I tried (but didn't work)</p>