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.

114 lines
4.2 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
from shutil import copyfile
from datetime import datetime
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')
#to refer backto zoomreplace.html
@app.route('/image/static/img/<imgpath>')
def zoomedimage(imgpath):
print (imgpath)
# list the files in imgpath[0:2], pass them to template
images = glob.glob('./static/img/' + imgpath[0:2] + '/*.png')
images.sort(reverse=True)
print(images)
return render_template("zoomreplace.html", image=imgpath, images=images)
@app.route('/upload2/<image>', methods=['POST'])
def upload2_file(image):
if request.method == 'POST':
file = request.files['tile']
if file and allowed_file(file.filename):
filename, fileextension = os.path.splitext(file.filename)
overwrite = image #00.png
input_file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename + fileextension)
overwritten_file_path = os.path.join(app.config['UPLOAD_FOLDER'], overwrite)
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)
# save it in the main folder with the number as the name
file.save(overwritten_file_path)
# and save it in the folder with the date as the name
number_folder = image[0:2]
datething = str(datetime.now()).replace(":", "").replace(" ", "").replace("-", "").replace(".", "")
print(datething)
memory_file_path = os.path.join(app.config['UPLOAD_FOLDER'],number_folder, datething + ".png")
print(memory_file_path)
# tempfile.save(memory_file_path)
copyfile(overwritten_file_path, memory_file_path)
return redirect("/")