Sigils
Sigils

Reputation: 2562

Flask-RESTful - Upload image

I was wondering on how do you upload files by creating an API service?

class UploadImage(Resource):
    def post(self, fname):
        file = request.files['file']
        if file:
            # save image
        else:
            # return error
            return {'False'}

Route

api.add_resource(UploadImage, '/api/uploadimage/<string:fname>')

And then the HTML

   <input type="file" name="file">

I have enabled CORS on the server side

I am using angular.js as front-end and ng-upload if that matters, but can use CURL statements too!

Upvotes: 28

Views: 68562

Answers (6)

Sushang Agnihotri
Sushang Agnihotri

Reputation: 666

#Image(s) and file Upload
from flask import Flask, json, request, jsonify
import os
import urllib.request
from werkzeug.utils import secure_filename

app = Flask(__name__)

app.secret_key = "caircocoders-ednalan"

UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/api/upload', methods=['POST'])
def upload_image():
    if 'image' not in request.files:
        resp = jsonify({
            'status' : False,
            'message' : 'Image is not defined'})
        resp.status_code = 400
        return resp

    files = request.files.getlist('image')

    errors = {}
    success = False

for photo in files:

    if photo and allowed_file(photo.filename):
        filename = secure_filename(photo.filename)
        photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        success = True
    else:
        errors[photo.filename] = 'Image type is not allowed'

if success and errors:
    errors['message'] = jsonify({
        'data' : photo.filename,
        'status' : True,
        'message' : 'Image(s) successfully uploaded'})
    resp = jsonify(errors)
    resp.status_code = 500
    return resp

if success:
    resp = jsonify({
        'data' : photo.filename,
        'status' : True,
        'message' : 'Images successfully uploaded'})
    resp.status_code = 201
    return resp
else:
    resp = jsonify(errors)
    resp.status_code = 500
    return resp

Upvotes: 0

Nikhil Shaw
Nikhil Shaw

Reputation: 39

Following is a simple program to upload an image using curl and saving it locally.

from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug
import cv2
import numpy as np

app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('file',
                    type=werkzeug.datastructures.FileStorage,
                    location='files',
                    required=True,
                    help='provide a file')

class SaveImage(Resource):
    def post(self):
        args = parser.parse_args()
        # read like a stream
        stream = args['file'].read()
        # convert to numpy array
        npimg = np.fromstring(stream, np.uint8)
        # convert numpy array to image
        img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
        cv2.imwrite("saved_file.jpg", img)

api.add_resource(SaveImage, '/image')

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

You can like using curl like this:

curl localhost:port/image -F file=@image_filename.jpg

Reference

Upvotes: 4

Ron Harlev
Ron Harlev

Reputation: 16663

The following is enough to save the uploaded file:

from flask import Flask
from flask_restful import Resource, Api, reqparse
import werkzeug

class UploadImage(Resource):
   def post(self):
     parse = reqparse.RequestParser()
     parse.add_argument('file', type=werkzeug.datastructures.FileStorage, location='files')
     args = parse.parse_args()
     image_file = args['file']
     image_file.save("your_file_name.jpg")

Upvotes: 28

Jethro Guce
Jethro Guce

Reputation: 69

you can use the request from flask

class UploadImage(Resource):
    def post(self, fname):
        file = request.files['file']
        if file and allowed_file(file.filename):
            # From flask uploading tutorial
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
        else:
            # return error
            return {'False'}

http://flask.pocoo.org/docs/0.12/patterns/fileuploads/

Upvotes: 6

Sibelius Seraphini
Sibelius Seraphini

Reputation: 5593

class UploadWavAPI(Resource):
    def post(self):
        parse = reqparse.RequestParser()
        parse.add_argument('audio', type=werkzeug.FileStorage, location='files')

        args = parse.parse_args()

        stream = args['audio'].stream
        wav_file = wave.open(stream, 'rb')
        signal = wav_file.readframes(-1)
        signal = np.fromstring(signal, 'Int16')
        fs = wav_file.getframerate()
        wav_file.close()

You should process the stream, if it was a wav, the code above works. For an image, you should store on the database or upload to AWS S3 or Google Storage

Upvotes: 23

iJade
iJade

Reputation: 23791

Something on the lines of the following code should help.

 @app.route('/upload', methods=['GET', 'POST'])
 def upload():
    if request.method == 'POST':
        file = request.files['file']
        extension = os.path.splitext(file.filename)[1]
        f_name = str(uuid.uuid4()) + extension
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name))
        return json.dumps({'filename':f_name})

Upvotes: 4

Related Questions