Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用流而不是瓶子中的静态文件返回图像_Python_Stream_Bottle - Fatal编程技术网

Python 使用流而不是瓶子中的静态文件返回图像

Python 使用流而不是瓶子中的静态文件返回图像,python,stream,bottle,Python,Stream,Bottle,我有一个简单的服务器应用程序,它是用Python编写的,使用瓶子框架。在一个路由中,我创建了一个图像并将其写入流,我希望将其作为响应返回。我知道如何使用static_file函数返回图像文件,但这对我来说很昂贵,因为我需要先将图像写入文件。我想直接使用stream对象为图像提供服务。我该怎么做 我当前的代码如下(文件版本): 与此相反,我想做如下事情: @route('/image') def video_image(): image_buffer = BytesIO() pi_

我有一个简单的服务器应用程序,它是用Python编写的,使用瓶子框架。在一个路由中,我创建了一个图像并将其写入流,我希望将其作为响应返回。我知道如何使用static_file函数返回图像文件,但这对我来说很昂贵,因为我需要先将图像写入文件。我想直接使用stream对象为图像提供服务。我该怎么做

我当前的代码如下(文件版本):

与此相反,我想做如下事情:

@route('/image')
def video_image():
    image_buffer = BytesIO()
    pi_camera.capture(image_buffer, format='jpeg') # This works without a problem

    # What to write here?
只需返回字节。(还应设置内容类型标题。)

@route('/image')
def video_image():
    image_buffer = BytesIO()
    pi_camera.capture(image_buffer, format='jpeg') # This works without a problem

    # What to write here?
@route('/image')
def video_image():
    image_buffer = BytesIO()
    pi_camera.capture(image_buffer, format='jpeg') # This works without a problem

    image_buffer.seek(0) # this may not be needed
    bytes = image_buffer.read()
    response.set_header('Content-type', 'image/jpeg')
    return bytes