I am working on a project involving removing the background from images. Here is my Python code.
from __future__ import print_function
from rembg import remove
from PIL import Image
import easygui as eg
from flask import Flask, jsonify, render_template
app = Flask(__name__)
@app.route("/")
def getPage():
return render_template("index.html")
@app.route("/getPic")
def removeBackground():
try:
print("I come here 1")
input_path = eg.fileopenbox(title='Select image file')
print("I come here 2")
input = Image.open(input_path)
output = remove(input)
output_path = eg.filesavebox(title='Save file to..')
output.save(output_path)
data={
"inputPath": input_path,
"output": output
}
result = jsonify(data)
except Exception as e:
print("I am error")
result = 0
print(e)
return result
if __name__ == "__main__":
app.run(host='127.0.0.1', port=5000)
Here is my index.html code
<!DOCTYPE html>
<html>
<body>
<button onclick="chooseFile()">Choose Image File</button>
<script>
function chooseFile() {
fetch("http://localhost:5000/getPic").then(function(x) {
console.log("success");
console.log(x);
});
}
</script>
</body>
</html>
When I try to test this on localhost:5000, the page loads and I can click the button. Using print statements, I have verified that the button click does call the @app.route("/getPic") endpoint. The statement "I come here 1" prints on the command line. However, the file selection dialog box usually does not open, and the statement "I come here 2" is never reached. Clearly, the offending line is this one:
input_path = eg.fileopenbox(title='Select image file')
However, it does not cause an exception to occur (as the code does not go into my Except block).
Does anyone know what the issue may be here with the above line or with the EasyGui module in general?