Image printing works

This commit is contained in:
TT-392 2024-09-15 14:57:26 +02:00
parent bd1c7ed477
commit 10d218bfba
5 changed files with 63 additions and 20 deletions

View file

@ -20,6 +20,7 @@ if not os.path.exists(app.config['UPLOAD_FOLDER']):
if not os.path.exists(app.config['TEXT_FOLDER']): if not os.path.exists(app.config['TEXT_FOLDER']):
os.makedirs(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']) @app.route('/image', methods=['GET', 'POST'])
def image(): def image():
@ -33,20 +34,40 @@ def image():
if file: if file:
extension = os.path.splitext(file.filename)[1] extension = os.path.splitext(file.filename)[1]
filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension) filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension)
session["filepath"] = filepath
file.save(filepath) file.save(filepath)
# Process image # # Process image #
height = Image.open(filepath).height format_image(filepath).save(filepath)
valid = False
if height == valid_height:
valid = True
################# #################
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 = { templates = {
"DNH": { "DNH": {
@ -128,7 +149,7 @@ def text():
@app.route('/text-print', methods=['GET', 'POST']) @app.route('/text-print', methods=['GET', 'POST'])
def tex_print(): def tex_print():
if request.method == 'POST': if request.method == 'POST':
image = format_image("static/text/text.png") image = format_image_to_label("static/text/text.png")
print_image(image) print_image(image)
if 'cut' in request.form: if 'cut' in request.form:

View file

@ -1,12 +1,27 @@
from PIL import Image from PIL import Image
from printer_info import *
def format_image(path): def format_image_to_label(path):
image = Image.open(path) image = Image.open(path)
image = image.rotate(-90, expand=True) 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)) new_image.paste(image, (0, 0))
return new_image 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")

View file

@ -1,5 +1,5 @@
dev_path="/dev/usb/lp0" dev_path="/dev/usb/lp0"
from format_image import format_image from format_image import *
from PIL import Image from PIL import Image
import os import os

View file

@ -1 +1,2 @@
label_width = 100 label_width = 100
printer_width = 512

View file

@ -2,19 +2,25 @@
{% block content %} {% block content %}
<h1>Upload Image</h1> <h1>Upload Image</h1>
<form method="POST" enctype="multipart/form-data"> <form method="POST" action="/image" enctype="multipart/form-data">
<input type="file" name="image"> <input type="file" name="image">
<input type="submit" value="Upload"> <input type="submit" value="Upload">
</form> </form>
{% if filename %} {% if filename %}
<h2>Uploaded Image:</h2> <br><br>
<img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image" style="max-width:300px;"> <img style="border: 1px solid blueviolet;"
{% if valid %} src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
<p style="color: green;">Height: {{ height }} px</p>
{% else %} <form method="POST" action="/image-print">
<p style="color: red;">Height: {{ height }} px</p> {% if cut %}
{% endif %} <input type="checkbox" name="cut" value="cut" checked>
{%else %}
<input type="checkbox" name="cut" value="cut">
{%endif%}
<label for="cut"> Cut paper after printing </label><br>
<input type="submit" value="Print" style="font-size: 2em; padding: 20px 40px;">
{% endif %} {% endif %}
{% endblock %} {% endblock %}