0

I have a system to load image files from html but once I select my file and load it from the front end I need a python api to read that image file I select. How can I connect python to my html? So, to be specific it would be to load an image file with html and have my python api (I use flask) download it and read it to extract data.enter image description here

The html to load the file would be:

 <form action="" method="POST" enctype=multipart/form-data>
                        <div class="col-md-6">
                        <input type="file" name="file">
                        <input class="button button-block button-primary" type="submit" value="Upload">
                        </div>
                    </form>

The python API to download and read the file would be:

import os
from flask import Flask, render_template, request
from werkzeug import secure_filename
from PIL import Image

# instance of the Flask object
app = Flask(__name__)
# Carpeta de subida
app.config['UPLOAD_FOLDER'] = './Archivos GTiff'


@app.route("/")
@app.route("imgeo/upload-img.html")
def upload_file():
    # render the template "upload-img.html"
    return render_template('upload-img.html')


@app.route('imgeo/upload-img.html')
def viewer():
    """Show a map to view the GeoJSON returned from a ImGeo endpoint"""
    return render_template('imgeo/upload-img.html')


@app.route("imgeo/upload-img.html", methods=['POST'])
def uploader():
    if request.method == 'POST':
        # we get the file from the "file" input
        f = request.files['archivo']
        filename = secure_filename(f.filename)
        # Save the file in the directory "tiff files".
        f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # We return a satisfactory answer
        return "<h1>File uploaded successfully</h1>"


if __name__ == '__main__':
    # Start the application
    app.run(debug=True)
Justinas
  • 41,402
  • 5
  • 66
  • 96
mona 2
  • 13
  • 2

1 Answers1

0

You could try to encode your image in base64 with JavaScript, send the result with a POST request, and then (in Python) decode it. Check out these two threads:

How can I convert an image into Base64 string using JavaScript? (JavaScript)

Convert string in base64 to image and save on filesystem (Python)

Here is a similar question too:

Sending an image from html to python script through CGI

Jonas Petri
  • 16
  • 1
  • 1