forked from emilevs/label_printer
Image printing works
This commit is contained in:
parent
bd1c7ed477
commit
10d218bfba
5 changed files with 63 additions and 20 deletions
37
flask/app.py
37
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:
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
label_width = 100
|
||||
printer_width = 512
|
||||
|
|
|
@ -2,19 +2,25 @@
|
|||
|
||||
{% block content %}
|
||||
<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="submit" value="Upload">
|
||||
</form>
|
||||
|
||||
{% if filename %}
|
||||
<h2>Uploaded Image:</h2>
|
||||
<img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image" style="max-width:300px;">
|
||||
{% if valid %}
|
||||
<p style="color: green;">Height: {{ height }} px</p>
|
||||
{% else %}
|
||||
<p style="color: red;">Height: {{ height }} px</p>
|
||||
{% endif %}
|
||||
<br><br>
|
||||
<img style="border: 1px solid blueviolet;"
|
||||
src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
|
||||
|
||||
<form method="POST" action="/image-print">
|
||||
{% if cut %}
|
||||
<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 %}
|
||||
{% endblock %}
|
||||
|
||||
|
|
Loading…
Reference in a new issue