diff --git a/flask/app.py b/flask/app.py index d203deb..fae4edb 100644 --- a/flask/app.py +++ b/flask/app.py @@ -20,6 +20,7 @@ if not os.path.exists(app.config['UPLOAD_FOLDER']): if not os.path.exists(app.config['TEXT_FOLDER']): os.makedirs(app.config['TEXT_FOLDER']) +#TODO: make it so sessions don't interfere with eachother @app.route('/image', methods=['GET', 'POST']) def image(): @@ -33,20 +34,40 @@ def image(): if file: extension = os.path.splitext(file.filename)[1] filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension) + session["filepath"] = filepath file.save(filepath) # Process image # - height = Image.open(filepath).height - - valid = False - if height == valid_height: - valid = True + format_image(filepath).save(filepath) ################# + + session["filename"] = "upload"+extension + return render_template('image.html', filename=session["filename"], cut=session["cut"]) - return render_template('image.html', filename="upload"+extension, height=height, valid=valid) + session["cut"] = True + return render_template('image.html', cut=True) + + +#TODO: maybe merge some stuff with text-print +@app.route('/image-print', methods=['GET', 'POST']) +def text_print(): + if request.method == 'POST': + image = format_image_to_label(session["filepath"]) + print_image(image) + + if 'cut' in request.form: + print("printing and cutting") + cut_paper() + session["cut"] = True; + else: + print("printing") + session["cut"] = False; + + return render_template('image.html', filename=session["filename"], cut=session["cut"]) + + return render_template('image.html', filename=session["filename"], cut=session["cut"]) - return render_template('image.html') templates = { "DNH": { @@ -128,7 +149,7 @@ def text(): @app.route('/text-print', methods=['GET', 'POST']) def tex_print(): if request.method == 'POST': - image = format_image("static/text/text.png") + image = format_image_to_label("static/text/text.png") print_image(image) if 'cut' in request.form: diff --git a/flask/format_image.py b/flask/format_image.py index a80d6da..47efbf2 100644 --- a/flask/format_image.py +++ b/flask/format_image.py @@ -1,12 +1,27 @@ from PIL import Image +from printer_info import * -def format_image(path): +def format_image_to_label(path): image = Image.open(path) image = image.rotate(-90, expand=True) - new_image = Image.new('1', (512, image.height), 1) + new_image = Image.new('1', (printer_width, image.height), 1) new_image.paste(image, (0, 0)) return new_image -#format_image("static/text/text.png").save("output.png") +def format_image(path): + image = Image.open(path) + + if (image.height != label_width): + print("resizing image") + new_height = label_width + new_width = int(label_width * image.width / image.height) + + image = image.resize((new_width, new_height)) + + image = image.convert('1', dither=Image.FLOYDSTEINBERG) + return image + + +format_image("static/uploads/upload.png").save("output.png") diff --git a/flask/print.py b/flask/print.py index 0effc24..795f682 100644 --- a/flask/print.py +++ b/flask/print.py @@ -1,5 +1,5 @@ dev_path="/dev/usb/lp0" -from format_image import format_image +from format_image import * from PIL import Image import os diff --git a/flask/printer_info.py b/flask/printer_info.py index 2da3a4e..7f2796a 100644 --- a/flask/printer_info.py +++ b/flask/printer_info.py @@ -1 +1,2 @@ label_width = 100 +printer_width = 512 diff --git a/flask/templates/image.html b/flask/templates/image.html index 6027733..f7060b6 100644 --- a/flask/templates/image.html +++ b/flask/templates/image.html @@ -2,19 +2,25 @@ {% block content %}
Height: {{ height }} px
- {% else %} -Height: {{ height }} px
- {% endif %} +