"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

August 07, 2022

Flask - Image Attachment - Samples - API

 

import flask
from flask import Flask, request, jsonify
from PIL import Image
from flask_cors import CORS, cross_origin
from flask import send_file
app = flask.Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app)
app.config["DEBUG"] = True
#Receive image
#Respond with return same image
@app.route('/sendimagetoserver',methods=['POST'])
def fetch_legging_recommendation():
file = request.files['image']
img = Image.open(file.stream)
source_file_name = 'Inputimage.jpg'
img = img.save(source_file_name)
return send_file(source_file_name, mimetype='image/jpg')
#For mac
#app.run(host= '0.0.0.0',port=4321,threaded=True)
#For windows
app.run(host= 'localhost',port=4321,threaded=True)
import requests
def testmethod():
url = 'http://127.0.0.1:4321/sendimagetoserver'
my_img = {'image': open(r'D1.jpg','rb')}
response = requests.post(url,files=my_img,timeout=20)
if response.status_code == 200:
with open("result_sample.jpg", 'wb') as f:
f.write(response.content)
print(response)
testmethod()

Keep Exploring!!!!

No comments: