Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am creating an API to receive and process images. I have to receive the image in bytearray format. The following is my code to post:

Approach 1 Posting the image to api

with open("test.jpg", "rb") as imageFile:
    f = imageFile.read()
    b = bytearray(f)    
    url = 'http://127.0.0.1:5000/lastoneweek'
    headers = {'Content-Type': 'application/octet-stream'}
    res = requests.get(url, data=b, headers=headers)
    ##print received json response
    print(res.text)

My API: Receiving image at api

@app.route('/lastoneweek', methods=['GET'])
def get():
    img=request.files['data']
    image = Image.open(io.BytesIO(img))
    image=cv2.imread(image)
    ##do all image processing and return json response

Within my api I have tried, request.get['data'] request.params['data']....I am getting object has no attribute error.

I tried passing the bytearray to json along with width and height of the image like:

Approach 2:Posting image to api

data = '{"IMAGE":b,"WIDTH":16.5,"HEIGHT":20.5}'
url = 'http://127.0.0.1:5000/lastoneweek'
headers = {'Content-Type': 'application/json'}
res = requests.get(url, data=data, headers=headers)

and changed my get function at the API as

Receive image at api

@app.route('/lastoneweek', methods=['GET'])
def get():
    data=request.get_json()
    w = data['WIDTH']
    h = data['HEIGHT']

but have received the following error for example:

TypeError: 'LocalProxy' does not have the buffer interface
    print(request.files['image_data'])
    img = request.files['image_data']
    image = cv2.imread(img.filename)
    rows, cols, channels = image.shape
    M = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
    dst = cv2.warpAffine(image, M, (cols, rows))
    cv2.imwrite('output.png', dst)
    ##do all image processing and return json response
    return 'image: success'
if __name__ == '__main__':
        app.run()
    except Exception as e:
        print(e)

with client.py file as:

import requests
with open("test.png", "rb") as imageFile:
    # f = imageFile.read()
    # b = bytearray(f)    
    url = 'http://127.0.0.1:5000/lastoneweek'
    headers = {'Content-Type': 'application/octet-stream'}
        response = requests.post(url, files=[('image_data',('test.png', imageFile, 'image/png'))])
        print(response.status_code)
        print(response.json())
    except Exception as e:
        print(e)
    # res = requests.put(url, files={'image': imageFile}, headers=headers)
    # res = requests.get(url, data={'image': imageFile}, headers=headers)
    ##print received json response
    print(response.text)

I referred this link: http://docs.python-requests.org/en/master/user/advanced/#post-multiple-multipart-encoded-files This solves the first issue.

The line image = Image.open(io.BytesIO(img)) is wrong since img is a <class 'werkzeug.datastructures.FileStorage'> which should not be passed to io.BytesIO, since it takes bytes-like object as mentioned here: https://docs.python.org/3/library/io.html#io.BytesIO, and explanation of bytes-like object here: https://docs.python.org/3/glossary.html#term-bytes-like-object So, instead of doing this. Passing filename directly to cv2.imread(img.filename) solved the issue.

Thank you...my original development is quite similar to the one that you have provided...which is posting the image and receiving the other end. It is working without trouble. However, the client can send the image only as bytearray and hence the question...thank you again for citing the help links...I will go through them. – Apricot Jul 16, 2018 at 17:42 Thank you and apologies....I misssed the 'rb' parameter and was under the impression that code you have provided just reads and pushes the file...this works perfectly. Thank you again. – Apricot Jul 19, 2018 at 3:32 I am learning Flask and your post and comment helped me to learn further about it, so Thank You. – thelogicalkoan Jul 19, 2018 at 12:28 When I am trying to post the image as bytearray to a 127.0.0.1:5000 server I get read the image in the server with opencv...however when I change the path to a remote server...the image is not getting delivered...do you have any thoughts... – Apricot Jul 20, 2018 at 6:31

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.