Need to share screen under special circumstances? Microscreen sharing server based on flash (can realize screen sharing)

Keywords: Python OpenCV


Today, I'd like to share an interesting little project with you. I need to make a report a long time ago. There was something wrong with the original screen sharing software, so I temporarily wrote this program to deal with the emergency. The effect is good. Today, I'd like to share it with you. In case of some special circumstances, it can be used to deal with the emergency

1, Use environment
Pillow 6.0.0
flask 1.1.1
2, Project introduction

In fact, it's not very difficult. Before I used the UDP broadcast shared screen, now we have a library in python with pilot to get the screen, which can realize the screenshot effect and convert the picture into a byte stream. But pilot has a disadvantage. If the screen is scaled, it's scaled, which means that the screen picture you get is not a complete picture. Next, I will introduce How to use Python to capture a screen and convert it to a byte stream:

  • Pillow
from PIL import ImageGrab

img = ImageGrab.grab()"a.png")

Pilot can't capture full screen in case of screen zooming

  • pyautogui
import pyautogui

img = pyautogui.screenshot()"a.png")

pyautogui can also get full screen when the screen is zoomed

  • Picture to byte stream
import pyautogui
from io import BytesIO

img = pyautogui.screenshot()
output_buffer = BytesIO()  # Creating binary objects, format='JPEG', quality=100)  # quality improves image resolution
frame = output_buffer.getvalue()  # Get binary data

2, Hands on practice

  • Create a new folder (do it with me)
  • Create another templates folder in the folder
  • Create an index.html in templates
  • Create three empty Python files base ABCD
  • After creation, paste the following code into index.html
    <title>Screen sharing</title>
<h4>Screen sharing</h4>
<img src="{{ url_for('video_feed') }}">
  • Then paste the following code into: base?
import time
import threading
    from greenlet import getcurrent as get_ident
except ImportError:
        from thread import get_ident
    except ImportError:
        from _thread import get_ident

class CameraEvent(object):
    def __init__(self): = {}

    def wait(self):
        ident = get_ident()
        if ident not in
  [ident] = [threading.Event(), time.time()]

    def set(self):
        now = time.time()
        remove = None
        for ident, event in
            if not event[0].isSet():
                event[1] = now
                if now - event[1] > 5:
                    remove = ident
        if remove:

    def clear(self):[get_ident()][0].clear()

class BaseCamera(object):
    thread = None
    frame = None
    last_access = 0
    event = CameraEvent()

    def __init__(self):
        if BaseCamera.thread is None:
            BaseCamera.last_access = time.time()

            BaseCamera.thread = threading.Thread(target=self._thread)

            while self.get_frame() is None:

    def get_frame(self):
        BaseCamera.last_access = time.time()


        return BaseCamera.frame

    def frames():
        raise RuntimeError('Must be implemented by subclasses.')

    def _thread(cls):
        print('Starting camera thread.')
        frames_iterator = cls.frames()
        for frame in frames_iterator:
            BaseCamera.frame = frame
            if time.time() - BaseCamera.last_access > 10:
                print('Stopping camera thread due to inactivity.')
        BaseCamera.thread = None
  • Then paste the following code into:
from io import BytesIO
import cv2
from PIL import ImageGrab, Image
from base_camera import BaseCamera

class Camera(BaseCamera):
    video_source = 0

    def set_video_source(source):
        Camera.video_source = source

    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Error')

        while True:
            image = ImageGrab.grab()  # Get screen data
            # w, h = image.size
            image = image.resize((1366, 750), Image.ANTIALIAS)  # Picture zoom
            output_buffer = BytesIO()  # Creating binary objects
  , format='JPEG', quality=100)  # quality improves image resolution
            frame = output_buffer.getvalue()  # Get binary data
            yield frame  # Generator returns binary data of a picture
  • Then paste the following code into:
import os
from importlib import import_module
from flask import Flask, render_template, Response

if os.environ.get('CAMERA'):
    Camera = import_module('camera_' + os.environ['CAMERA']).Camera
    from camera_pil import Camera

app = Flask(__name__)

def index():
    //View function
    return render_template('index.html')

def gen(camera):
    //Streaming generator
    while True:
        frame = camera.get_frame()

        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

def video_feed():
    """Streaming data"""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':, host="", port=80)
  • Run, then open your browser and type: try it!
  • If you want to use it, please change the host = "" to host = "" and run the screen sharing server. Check the ip address of your Ethernet and tell the receiver to let them access your ip address using the browser. Then you can realize the screen sharing. You will find that your CPU will be crazy (not so exaggerated) after all, it is an emergency
Published 21 original articles, won praise 35, visited 5101
Private letter follow

Posted by eyespark on Thu, 13 Feb 2020 07:30:15 -0800