# -*- coding: utf-8 -*- from flask import Flask, request, jsonify, send_from_directory, Response, make_response import uuid import requests from PIL import Image from io import BytesIO app = Flask(__name__) # 设置静态文件夹(如果您想指定路径) app.config['UPLOAD_FOLDER'] = 'predict/output' host = 'http://192.168.60.2' uploadPath = 'predict/upload/' outputPath = 'predict/output/' # GET 请求接口示例 @app.route('/api/get_data', methods=['GET']) def get_data(): # 获取请求参数 param = request.args.get('param') # 返回 JSON 响应 data = jsonify({ "message": "GET 请求成功", "param": param }) response = make_response(data) # response.headers['Content-Type'] = 'text/plain; charset=utf-8' return response # GET 请求接口示例 @app.route('/api/postgisupdate', methods=['POST']) def postgisupdate(): # 获取请求参数 param = request.args.get('param') # 返回 JSON 响应 return jsonify({ "message": "POST 请求成功", "param": param }) # POST 上传图片 @app.route('/api/upload_image', methods=['POST']) def upload_image(): # 获取上传的文件 img1 = request.files['img1'] img2 = request.files['img2'] # 保存文件 filePath1 = uploadPath + str(uuid.uuid4()) + img1.filename filePath2 = uploadPath + str(uuid.uuid4()) + img2.filename img1.save(filePath1) img2.save(filePath2) return jsonify({ "message": "上传成功", "data": { "img1": filePath1, "img2": filePath2 } }) # POST 通过url保存图片到本地 @app.route('/api/save_image', methods=['POST']) def save_image(): # 获取图片路径 data = request.get_json() img1 = data['img1'] img2 = data['img2'] response1 = requests.get('http://192.168.60.63' + img1) if response1.status_code != 200: return jsonify({ "message": "图片1不存在", "data": { "img1": img1 } }) img1 = Image.open(BytesIO(response1.content)) img1Path = uploadPath + str(uuid.uuid4()) + '.png' img1.save(img1Path, format='PNG') response2 = requests.get('http://192.168.60.63' + img2) if response2.status_code != 200: return jsonify({ "message": "图片2不存在", "data": { "img2": img2 } }) img2 = Image.open(BytesIO(response2.content)) img2Path = uploadPath + str(uuid.uuid4()) + '.png' img2.save(img2Path, format='PNG') return jsonify({ "message": "保存成功", "data": { "img1": img1Path, "img2": img2Path, } }) # 访问静态文件 @app.route('/predict/') def view_file(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=6500)