def extract_image_from_mjpeg(stream):
    """Take in a MJPEG stream object, return the jpg from it."""
    data = b''
    for chunk in stream:
        data += chunk
        jpg_end = data.find(b'\xff\xd9')
        if jpg_end == -1:
            continue
        jpg_start = data.find(b'\xff\xd8')
        if jpg_start == -1:
            continue
        return data[jpg_start:jpg_end + 2]
