首页 > 解决方案 > 来自 request.get(url) 的 Python 树莓派 Image.open 图像

问题描述

我正在尝试从 URL 打开图像然后处理图像。

我正在获取的图像来自通过此端点的一个覆盆子凸轮

@app.route('/image')
def getImage():
    frame = video_camera.get_frame()
    return Response((b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n'),
                       mimetype='multipart/x-mixed-replace; boundary=frame')

然后在另一个覆盆子上,我试图以这种方式获取图像:

 r = requests.get('http://'+ip+'/image') 
 curr_img = Image.open(BytesIO(r.content))

如果我在浏览器中打开链接,我可以看到图像,所以那部分似乎没问题。但是在使用 Image.open 时仍然出现此错误:

OSError: cannot identify image file <_io.BytesIO object at 0xffff8836dba0>

任何想法?

标签: pythonraspberry-pi

解决方案


就我而言,我需要改变我的

@app.route('/image')
def getImage():
    frame = video_camera.get_frame()
    return Response((b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n'),
                       mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/image')
def getImage():
    frame = video_camera.get_frame()
    return frame

我的 video_camera.get_frame() 已经给了我图像的字节,所以我不需要添加任何内容。


推荐阅读