flask接收图片、视频

from flask import Flask,jsonify

app = Flask(__name__)


def imgStream(self, data, filename):
    filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'upfiles')# 保存图片路径
    result = imgmod.save().savefile(data, filepath, filename)
    return result

def imgShow(self, data):
    imgmod.save().imgshow(data)
    return 0


# 保存图片
@app.route('/getimg/', methods=['post'])
def getimg():
    try:
        fileObj = request.files['file'].stream.read()# file是上传的文件名
        imgStream(fileObj, request.files['file'].filename) # filename真实的文件名
        # imgShow(fileObj)
        return jsonify((dict(zip(("status", "message"), [0, 'success']))))
    except Exception as e:
        return jsonify((dict(zip(("status", "message"), [1, e]))))

# 保存视频
@app.route('/getmp4/', methods=['post'])
def getmp4():
    try:
        fileObj = request.files['file'].stream.read()
        imgStream(fileObj, request.files['file'].filename)
        return jsonify((dict(zip(("status", "message"), [0, 'success']))))
    except Exception as e:
        return jsonify((dict(zip(("status", "message"), [1, e]))))


import base64

# 保存64编码图片
@app.route('/getpic/', methods=['post'])
def getpic():
    try:

        fileObj = request.files['file'].stream.read()
        imgdata = base64.b64decode(fileObj)
        op.imgStream(imgdata, request.files['file'].filename)
        return jsonify((dict(zip(("status", "message"), [0, 'success']))))
    except Exception as e:
        return jsonify((dict(zip(("status", "message"), [1, e]))))

if __name__ == '__main__':
    app.run()

 

imagemod

from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt


class save(object):

    # 保存图片
    def savefile(self, data, filepath, filename):
        file = filepath + '\\' + filename
        with open(file, 'wb') as f:
            f.write(data)
        return "success"


    # 展示图片
    def imgshow(self, data):
        img = Image.open(BytesIO(data)).convert('RGB')
        plt.imshow(img)
        plt.show()

 requests提交

https://blog.csdn.net/qq_40155090/article/details/108441261

你可能感兴趣的:(flask,python,flask,接收图片,接收视频)