[Flask] image에 대해 주고 받기

[Flask] image에 대해 주고 받기

728x90

반응형

server.py

from flask import Flask, jsonify, request from PIL import Image import json from io import BytesIO import base64 app = Flask(__name__) @app.route("/", methods=["GET", "POST"]) def index(): json_data = request.get_json() dict_data = json.loads(json_data) img = dict_data['img'] img = base64.b64decode(img) img = BytesIO(img) img = Image.open(img) # img.save('test.jpg') img_shape = img.shape return img_shape if __name__== "__mmain__": app.debug = True app.run(host="", port=<포트 번호>)

client.py

import requests import json import base64 import cv2 image_name = 'test.jpg' img = cv2.imread(image_name) jpg_img = cv2.imencode('.jpg', img) b64_string = base64.b64encode(jpg_img[1]).decode('utf-8') files = { "img": b64_string, } r = requests.post("<서버 주소>", json=files) print(r.json())

728x90

반응형

from http://shuka.tistory.com/30 by ccl(A) rewrite - 2021-12-13 11:00:51