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.
129 lines
4.8 KiB
Python
129 lines
4.8 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, move
|
|
from datetime import datetime
|
|
|
|
UPLOAD_FOLDER = os.path.join("static", "img")
|
|
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mp3', 'm4a', 'ogg', 'ogv', 'mp4', 'mkv'}
|
|
def allowed_file(filename):
|
|
return '.' in filename and \
|
|
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
def lastupload(imgpath):
|
|
files = glob.glob('./static/img/' + imgpath[0:2] + '/*')
|
|
files.sort(reverse=True)
|
|
return files[0]
|
|
|
|
# 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():
|
|
lastfiles = []
|
|
for i in range (0,42):
|
|
last=lastupload(str(i).zfill(2))
|
|
lastfiles.append(last)
|
|
images = glob.glob('./static/img/*.png')
|
|
images.sort()
|
|
print(images)
|
|
print(lastfiles)
|
|
|
|
return render_template("mosaic2.html", lastfiles=lastfiles, 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'], overwrite + '/' + filename + fileextension)
|
|
overwritten_file_path = os.path.join(app.config['UPLOAD_FOLDER'], overwrite + fileextension)
|
|
|
|
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(input_file_path)
|
|
|
|
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
|
|
files = glob.glob('./static/img/' + imgpath[0:2] + '/*')
|
|
files.sort(reverse=True)
|
|
print(files)
|
|
return render_template("zoomreplace2.html", image=imgpath, files=files )
|
|
|
|
@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 = os.path.splitext(image)[0] #00 . png
|
|
|
|
input_file_path = os.path.join(app.config['UPLOAD_FOLDER'], overwrite + '/' + filename + fileextension)
|
|
# overwritten_file_path = os.path.join(app.config['UPLOAD_FOLDER'], overwrite + fileextension)
|
|
|
|
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(input_file_path)
|
|
|
|
# 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 + fileextension)
|
|
|
|
print(memory_file_path)
|
|
# tempfile.save(memory_file_path)
|
|
move(input_file_path, memory_file_path)
|
|
|
|
return redirect("/breadcube/overlap/")
|