You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.3 KiB
Python

# This is a flask application that overlaps uploaded images and text on top of images laid out in a grid on the browser.
from flask import Flask
from flask import request, redirect
from flask import render_template
import glob
import os
UPLOAD_FOLDER = os.path.join("static", "img")
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# create flask application
app = Flask(__name__)
# configure the upload folder
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# load the settings of the applicaiton
# (to handle the routing correctly)
app.config.from_pyfile('settings.py')
@app.route("/")
def overlap():
images = glob.glob('./static/img/*.png')
images.sort()
print(images)
return render_template("mosaic.html", images=images)
from flask import request
@app.route('/upload/', methods=['POST'])
def upload_file():
if request.method == 'POST':
file = request.files['tile']
if file and allowed_file(file.filename):
filename, fileextension = os.path.splitext(file.filename)
overwrite = request.form["overwrite"]
input_file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename + fileextension)
overwritten_file_path = os.path.join(app.config['UPLOAD_FOLDER'], overwrite + ".png")
print("[input file path]:", input_file_path)
print("[overwritten file path]:", overwritten_file_path)
if 'png' not in fileextension:
print("--- convert uploaded image to png")
cmd = f"convert { input_file_path } { overwritten_file_path }"
os.system(cmd)
file.save(overwritten_file_path)
# print("==============================================================")
# print(fileextension)
# print(f"copy { UPLOAD_FOLDER + file.filename} { UPLOAD_FOLDER + overwrite + fileextension }")
# os.system(f"copy { UPLOAD_FOLDER + file.filename} { UPLOAD_FOLDER + overwrite + fileextension }")
# print(f"del { UPLOAD_FOLDER + file.filename }")
# os.system(f"del { UPLOAD_FOLDER + file.filename }")
return redirect("/")
# f = request.files['the_file']
# f.save('/var/www/uploads/uploaded_file.txt')