I have a portion of code like this:
@app.route('/', methods=['POST'])
def predict():
  
   if flask.request.method == 'POST':
           #global mymodel
           first  =datetime.now()
           f = request.files["img"].read()
           f = Image.open(io.BytesIO(f))
This code predicts whether an image is Spam or not. I want to RaiseCustomError with a messsage Please upload an image But for this I need to detect the data type of the incoming ByteString. I have thought about using the  f = Image.open(io.BytesIO(f)) inside a try block so when an error occurs, it can perform
exception as e:
    return e
but that is stupid to do in my own opinion.
How can I detect the type of data from POST request so that if it not something belonging to any of Image type, I can raise a custom error with a message.
`
 
    