Home > Blockchain >  Flask Image API returning an error constantly
Flask Image API returning an error constantly

Time:01-16

I have an image API for which this is the code:

@app.route("/identify-x-rays", methods=["POST"])
def process_image():
    file = request.files['image'] # Read the image via file.stream

    img = Image.open(io.BytesIO(file).stream.read()).convert('RGB')
    img.save(os.path.join("static/", file.filename))
    answer, color = api(file.name)

    return jsonify({'msg': 'success',
                    'payload': answer,
                    'color': color})

(Dont worry the "api" function is defined)

But this returns this error:

    img = Image.open(io.BytesIO(file).stream.read()).convert('RGB')
TypeError: a bytes-like object is required, not 'FileStorage'

Please help me I have been debugging this for literally days.

This is the code for sending to the API:

import requests

url = "https://****.up.railway.app/identify-x-rays"
files = {'image': open('C:/****/msaha/Downloads/index.jpg', 'rb')}

r = requests.post(url, files=files)

Python version 3.6 Pls help.

CodePudding user response:

Form the error you have posted it seem like that requests.files['image'] returns a FileStorage object not a bytes like object. You can open that image via Image.open(requests.files['image']).

CodePudding user response:

WARNING: This is a development server. Do not use it in a production deployment.

Use a production WSGI server instead.

  •  Tags:  
  • Related