0

I'm trying to open an image from an URL to then process the image.

The image I'm fetching comes from one raspberry cam through this endpoint

@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')

Then on another raspberry I'm trying to get the image this way:

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

If I open the link in the browser I can see the image, so that part seems to be okay. But I still get this error when using Image.open:

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

Any idea?

4
  • Try printing/dumping the first 20 bytes of r.content to see if it looks like a JPEG. Commented Jun 4, 2020 at 13:40
  • it looks like this b'--frame\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x02\x01\x01\x01\x01\x01\x02\x01\x01\x01\x02\x02\x02\x02\x02\x04 and lot more of that basically Commented Jun 4, 2020 at 13:52
  • 1
    PIL Image is only going to like that from after the third \r\n onwards, i.e. starting ff d8 Commented Jun 4, 2020 at 13:56
  • Thanks, you "pushed me" to the right direction :) removed the extra bytes and now it opens the image properlly Commented Jun 4, 2020 at 17:18

1 Answer 1

1

In my case I needed to change my

@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')

to

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

My video_camera.get_frame() is already giving me the bytes of the image so I don't need to add nothing to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.