본문 바로가기
Development/Python

[Flask] image에 대해 주고 받기

by shuka 2021. 12. 13.
728x90
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="<ip>", 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
반응형

댓글