2024-09-17 18:12:24 +00:00
|
|
|
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
|
2024-09-14 13:29:37 +00:00
|
|
|
from PIL import Image
|
2024-09-14 18:49:57 +00:00
|
|
|
from gen_image import gen_image
|
|
|
|
from printer_info import *
|
|
|
|
from datetime import datetime
|
2024-09-15 10:41:12 +00:00
|
|
|
from print import *
|
|
|
|
from format_image import *
|
2024-09-17 18:12:24 +00:00
|
|
|
from file_handler import *
|
|
|
|
from colorama import Fore, Style
|
2024-09-14 13:29:37 +00:00
|
|
|
import os
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
generated_image_filename = "generated.png"
|
|
|
|
uploaded_image_filename = "uploaded" #extention is depended on uploaded file format
|
|
|
|
|
2024-09-14 13:29:37 +00:00
|
|
|
app = Flask(__name__)
|
2024-09-17 18:12:24 +00:00
|
|
|
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
app.secret_key = "blahaj"
|
2024-09-14 13:29:37 +00:00
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
templates = {
|
|
|
|
"DNH": {
|
|
|
|
"text1": {
|
|
|
|
"string": "Do Not Hack",
|
|
|
|
"size": 50,
|
|
|
|
"pos": 0
|
|
|
|
},
|
|
|
|
"text2": {
|
|
|
|
"string": "bottom text",
|
|
|
|
"size": 25,
|
|
|
|
"pos": 55
|
|
|
|
},
|
|
|
|
"cut": True
|
|
|
|
},
|
|
|
|
"Food": {
|
|
|
|
"text1": {
|
|
|
|
"string": "Nickname",
|
|
|
|
"size": 50,
|
|
|
|
"pos": 0
|
|
|
|
},
|
|
|
|
"text2": {
|
|
|
|
"string": "",
|
|
|
|
"size": 25,
|
|
|
|
"pos": 55
|
|
|
|
},
|
|
|
|
"cut": True
|
|
|
|
}
|
|
|
|
}
|
2024-09-14 13:29:37 +00:00
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
|
2024-09-16 15:45:11 +00:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
|
|
def base():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
2024-09-16 15:45:11 +00:00
|
|
|
return render_template('base.html')
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
|
|
|
|
# gets triggered when the user_data folder for the user is created
|
|
|
|
@new_user_handler
|
|
|
|
def on_new_user(session):
|
|
|
|
print(Fore.BLUE + "new user with ID:", session["ID"], Style.RESET_ALL)
|
|
|
|
|
|
|
|
session["text image path"] = None
|
|
|
|
session["uploaded image path"] = None
|
|
|
|
session["cut"] = True
|
|
|
|
session["text1"] = templates["DNH"]["text1"]
|
|
|
|
session["text2"] = templates["DNH"]["text2"]
|
|
|
|
session["cut"] = templates["DNH"]["cut"]
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
@app.route('/image', methods=['GET', 'POST'])
|
|
|
|
def image():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
2024-09-14 13:29:37 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if 'image' not in request.files:
|
|
|
|
return redirect(request.url)
|
|
|
|
file = request.files['image']
|
|
|
|
|
|
|
|
if file.filename == '':
|
|
|
|
return redirect(request.url)
|
|
|
|
if file:
|
|
|
|
extension = os.path.splitext(file.filename)[1]
|
2024-09-17 18:12:24 +00:00
|
|
|
session["uploaded image path"] = uploaded_image_filename+extension
|
2024-09-14 13:29:37 +00:00
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
filepath = get_file_path(session, session["uploaded image path"])
|
2024-09-14 13:29:37 +00:00
|
|
|
file.save(filepath)
|
|
|
|
|
2024-09-15 12:57:26 +00:00
|
|
|
format_image(filepath).save(filepath)
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
2024-09-15 12:57:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
#TODO: maybe merge some stuff with text-print
|
|
|
|
@app.route('/image-print', methods=['GET', 'POST'])
|
2024-09-16 15:45:11 +00:00
|
|
|
def image_print():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
|
|
|
if session["uploaded image path"] == None:
|
|
|
|
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
|
|
|
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
|
|
|
|
2024-09-15 12:57:26 +00:00
|
|
|
if request.method == 'POST':
|
2024-09-17 18:12:24 +00:00
|
|
|
image = format_image_to_label(get_file_path(session, session["uploaded image path"]))
|
2024-09-15 12:57:26 +00:00
|
|
|
print_image(image)
|
|
|
|
|
|
|
|
if 'cut' in request.form:
|
|
|
|
print("printing and cutting")
|
|
|
|
cut_paper()
|
|
|
|
session["cut"] = True;
|
|
|
|
else:
|
|
|
|
print("printing")
|
|
|
|
session["cut"] = False;
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
2024-09-14 18:49:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/text-template', methods=['GET', 'POST'])
|
|
|
|
def text_template():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
template = templates[request.form["template"]]
|
|
|
|
if request.form["template"] == "Food":
|
|
|
|
template["text2"]["string"] = datetime.now().strftime('%Y-%m-%d')
|
|
|
|
|
|
|
|
session["text1"] = template["text1"]
|
|
|
|
session["text2"] = template["text2"]
|
|
|
|
session["cut"] = template["cut"]
|
|
|
|
|
|
|
|
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
@app.route('/text-form', methods=['GET', 'POST'])
|
|
|
|
def text_form():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
session["text1"] = {
|
|
|
|
"string": request.form["string1"],
|
|
|
|
"size": int(request.form["size1"]),
|
|
|
|
"pos": int(request.form["pos1"]),
|
|
|
|
}
|
|
|
|
session["text2"] = {
|
|
|
|
"string": request.form["string2"],
|
|
|
|
"size": int(request.form["size2"]),
|
|
|
|
"pos": int(request.form["pos2"]),
|
|
|
|
}
|
|
|
|
|
|
|
|
img = gen_image(label_width, session["text1"], session["text2"])
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
get_folder_path(session)
|
2024-09-14 18:49:57 +00:00
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
session["text image path"] = generated_image_filename
|
|
|
|
img.save(get_file_path(session, session["text image path"]))
|
|
|
|
print()
|
|
|
|
print(session["text image path"])
|
2024-09-14 18:49:57 +00:00
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
2024-09-14 18:49:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/text', methods=['GET', 'POST'])
|
|
|
|
def text():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
|
|
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
2024-09-14 18:49:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/text-print', methods=['GET', 'POST'])
|
2024-09-16 15:45:11 +00:00
|
|
|
def text_print():
|
2024-09-17 18:12:24 +00:00
|
|
|
check_for_new_user(session)
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
if request.method == 'POST':
|
2024-09-17 18:12:24 +00:00
|
|
|
if session["text image path"] == None:
|
|
|
|
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
|
|
|
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
|
|
|
|
|
|
|
|
|
|
|
image = format_image_to_label(get_file_path(session, session["text image path"]))
|
2024-09-15 10:41:12 +00:00
|
|
|
print_image(image)
|
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
if 'cut' in request.form:
|
|
|
|
print("printing and cutting")
|
2024-09-15 10:41:12 +00:00
|
|
|
cut_paper()
|
2024-09-14 18:49:57 +00:00
|
|
|
session["cut"] = True;
|
|
|
|
else:
|
|
|
|
print("printing")
|
|
|
|
session["cut"] = False;
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
2024-09-14 18:49:57 +00:00
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
|
|
|
|
@app.route('/user_data/<filename>')
|
|
|
|
def user_data(filename):
|
|
|
|
return send_from_directory(get_folder_path(session), filename)
|
2024-09-14 18:49:57 +00:00
|
|
|
|
2024-09-14 13:29:37 +00:00
|
|
|
|
|
|
|
@app.route('/uploads/<filename>')
|
|
|
|
def uploaded_file(filename):
|
|
|
|
return redirect(url_for('static', filename='uploads/' + filename))
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
|
2024-09-14 18:49:57 +00:00
|
|
|
@app.route('/text/<filename>')
|
|
|
|
def generated_file(filename):
|
|
|
|
return redirect(url_for('static', filename='text/' + filename))
|
|
|
|
|
2024-09-17 18:12:24 +00:00
|
|
|
|
2024-09-14 13:29:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True)
|
|
|
|
|