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