Python screen capture error
<p>I am trying to modify the code given <a href="https://blog.miguelgrinberg.com/post/video-streaming-with-flask" rel="nofollow">here</a> for screen streaming. In the above tutorial it was for reading images from disk whereas I am trying to take screenshots. I receive this error.</p>

<blockquote>
  <p>assert isinstance(data, bytes), 'applications must write bytes'
  AssertionError: applications must write bytes</p>
</blockquote>

<p>What changes should I make for it to work?</p>

<p>This is what I've done so far - </p>

<pre><code>&lt;br&gt;index.html&lt;br&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Video Streaming Demonstration&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Video Streaming Demonstration&lt;/h1&gt;
    &lt;img src="{{ url_for('video_feed') }}"&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><br>app.py<br></p>

<pre><code>#!/usr/bin/env python
from flask import Flask, render_template, Response
import time
# emulated camera
from camera import Camera

# Raspberry Pi camera module (requires picamera package)
# from camera_pi import Camera

app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


def gen(camera):
    """Video streaming generator function."""
    while True:
        time.sleep(0.1)
        frame = camera.get_frame()
        yield (frame)


@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, threaded=True)
</code></pre>

<p><br>camera.py<br></p>

<pre><code>from time import time

from PIL import Image
from PIL import ImageGrab
import sys

if sys.platform == "win32":
    grabber = Image.core.grabscreen

class Camera(object):

    def __init__(self):
        #self.frames = [open('shot0' + str(f) + '.png', 'rb').read() for f in range(1,61)]
        self.frames = [ImageGrab.grab() for f in range(1,61)]

    def get_frame(self):
        return self.frames[int(time()) % 3]
</code></pre>

<p>Full error : 
 <a href="http://pastebin.com/4691Npyk" rel="nofollow">Link</a></p>

<p>The response payload must be a sequence of bytes. In the example, the images returned are JPEGs as <code>bytes</code> objects. </p>

<p>However, the image returned by <code>ImageGrab.grab()</code> is some PIL image class instead of bytes. So, try saving the image as JPEG as <code>bytes</code>:</p>

<pre><code>import io
</code></pre>

<p>Take screenshot only for every iteration in gen:</p>

<pre><code>class Camera(object):
    def get_frame(self):
        frame = ImageGrab.grab()
        img_bytes = io.BytesIO()
        frame.save(img_bytes, format='JPEG')
        return img_bytes.getvalue()
</code></pre>

<p>gen function:</p>

<pre><code>def gen(camera):
    while True:
        time.sleep(0.1)
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
</code></pre>

python image flask