Image sending / receiving memo in Python (Flask)

Introduction

This time, I will make a note of one of the ways to receive images in the python framework flask. Specifically, it receives the image in a base64-encoded format, decodes it on the flask side, and restores the image. Then, use open-cv to convert the restored image to grayscale, encode it to base64 again, and make a note of it.

Source

The library to be used is as follows.

requirements.txt


flask
flask-cors
opencv-python
opencv-contrib-python

I want to handle multiple data in the request, so I will use json format.

[
    {
        id : 0
        Image : id4zFjyrkAuSI2vUBKVLP...(Base64 encoded image)
    },
    {
        id : 1
        Image : k75GN/oll7KUCulSpSM/S...(Base64 encoded image)
    }
]

main.py


from flask import Flask, jsonify, request
from flask_cors import CORS
import cv2
import numpy as np
import base64

app = Flask('flask-tesseract-api')
CORS(app)

@app.route("/image/", methods=["POST"])
def post():
    """
Convert image to grayscale
    """

    response = []

    for json in request.json:

        #Decode Image
        img_stream = base64.b64decode(json['Image'])
        
        #Convert to an array
        img_array = np.asarray(bytearray(img_stream), dtype=np.uint8)
        
        # open-Grayscale with cv
        img_gray = cv2.imdecode(img_array, 0)
   
        #Save conversion result
        cv2.imwrite('result.png', img_before)

        #Encode for saved file
        with open('result.png', "rb") as f:
            img_base64 = base64.b64encode(f.read()).decode('utf-8')
        
        #Boxed in response json
        response.append({'id':json['id'], 'result' : img_base64})
       
    return jsonify(response)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000,debug=True)

Summary

I was able to send multiple images encoded to flask as json format. With this, you can implement the function to handle multiple images at the same time.

Recommended Posts

Image sending / receiving memo in Python (Flask)
CG image quality evaluation memo in Python
Image format in Python
Image uploader in Flask
Tweet with image in Python
Image Processing Collection in Python
Image addition memo in reportlab
Implemented image segmentation in python (Union-Find)
Web application development memo in python
Get, post communication memo in Python
Hello World in Flask [Appropriate memo]
Python memo
python memo
Flask memo
python memo
Python memo
Python memo
Python memo
Try sending a SYN packet in Python
Page cache in Python + Flask with Flask-Caching
Launch a Flask app in Python Anywhere
How to adjust image contrast in Python
Easy image processing in Python with Pillow
Memo of pixel position operation for image data in Python (numpy, cv2)
Quadtree in Python --2
Python in optimization
CURL in python
[Python] Memo dictionary
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
python beginner memo (9.2-10)
python image processing
A memo that I wrote a quicksort in Python
Meta-analysis in Python
Unittest in python
How to use Python Image Library in python3 series
What is wheezy in the Docker Python image?
Flask Primer Memo
Epoch in Python
Get image URL using Flickr API in Python
Sudoku in Python
DCI in Python
quicksort in python
A memo about writing merge sort in Python
[Memo] I tried a pivot table in Python
Flask basic memo
nCr in python
N-Gram in Python
★ Memo ★ Python Iroha
Programming in python
Super easy! Python + Flask environment in Docker quickly
Image reading memo
Plink in Python
Constant in python
Create an image with characters in python (Japanese)
[Python] EDA memo
Python 3 operator memo
Lifegame in Python.
FizzBuzz in Python