Compare commits

..

3 commits

9 changed files with 113 additions and 39 deletions

28
README.md Normal file
View file

@ -0,0 +1,28 @@
## steps I have taken while installing on pi
git clone [this repo] (I actually used scp)
sudo apt install python3-flask
rm -rf library_bridge.h and library_bridge.h
add: export GO111MODULE=on, to the first line of the Makefile
make
edit dhcpcd.conf and uncomment and change:
static ip_address=10.209.10.2/24
static routers=10.209.10.254
static domain_name_servers=10.209.10.255
edit /etc/nginx/sites-available/default and change:
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect http://127.0.0.1:8080 $scheme://$host:8080;
}
to
location / {
proxy_pass http://127.0.0.1:5000;
proxy_redirect http://127.0.0.1:8080 $scheme://$host:5000;
}
systemctl stop & enable labelapi.service
cp to /etc/systemd/system label_printer.service, and enable

67
app.py
View file

@ -2,16 +2,16 @@
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
from PIL import Image from PIL import Image
from gen_image import gen_image from gen_image import gen_image
from printer_info import * from config import *
from datetime import datetime from datetime import datetime
from print import * from print import *
from format_image import * from process_image import *
from file_handler import * from file_handler import *
from colorama import Fore, Style from colorama import Fore, Style
import os import os
generated_image_filename = "generated.png" text_image_filename = "text_image.png"
uploaded_image_filename = "uploaded" #extention is depended on uploaded file format uploaded_image_filename = "uploaded_image.png"
app = Flask(__name__) app = Flask(__name__)
@ -48,6 +48,15 @@ templates = {
} }
def render_text_template(info=None, info_color=None):
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"], info=info, info_color=info_color)
def render_image_template(info=None, info_color=None):
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"], info=info, info_color=info_color, label_width=label_width)
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def base(): def base():
check_for_new_user(session) check_for_new_user(session)
@ -81,14 +90,24 @@ def image():
return redirect(request.url) return redirect(request.url)
if file: if file:
extension = os.path.splitext(file.filename)[1] extension = os.path.splitext(file.filename)[1]
session["uploaded image path"] = uploaded_image_filename+extension
filepath = get_file_path(session, session["uploaded image path"]) try:
file.save(filepath) message, status, img = process_image(file.stream)
session["uploaded image path"] = uploaded_image_filename
img.save(get_file_path(session, session["uploaded image path"]))
except Exception as e:
message = f"{e}"
status = "Error"
format_image(filepath).save(filepath)
if status == "Error":
session["uploaded image path"] = None
return render_image_template("Error: " + message, "red")
elif status == "Info":
return render_image_template("Info: " + message, "black")
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"]) return render_image_template()
#TODO: maybe merge some stuff with text-print #TODO: maybe merge some stuff with text-print
@ -112,7 +131,7 @@ def image_print():
print("printing") print("printing")
session["cut"] = False; session["cut"] = False;
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"]) return render_image_template()
@app.route('/text-template', methods=['GET', 'POST']) @app.route('/text-template', methods=['GET', 'POST'])
@ -128,7 +147,7 @@ def text_template():
session["text2"] = template["text2"] session["text2"] = template["text2"]
session["cut"] = template["cut"] session["cut"] = template["cut"]
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_text_template()
@app.route('/text-form', methods=['GET', 'POST']) @app.route('/text-form', methods=['GET', 'POST'])
@ -147,23 +166,27 @@ def text_form():
"pos": int(request.form["pos2"]), "pos": int(request.form["pos2"]),
} }
img = gen_image(label_width, session["text1"], session["text2"]) message, status, img = gen_image(label_width, session["text1"], session["text2"])
if status == "Error":
session["text image path"] = None
return render_text_template(message, "red")
get_folder_path(session) get_folder_path(session)
session["text image path"] = generated_image_filename session["text image path"] = text_image_filename
img.save(get_file_path(session, session["text image path"])) img.save(get_file_path(session, session["text image path"]))
print() print()
print(session["text image path"]) print(session["text image path"])
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_text_template()
@app.route('/text', methods=['GET', 'POST']) @app.route('/text', methods=['GET', 'POST'])
def text(): def text():
check_for_new_user(session) check_for_new_user(session)
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_text_template()
@app.route('/text-print', methods=['GET', 'POST']) @app.route('/text-print', methods=['GET', 'POST'])
@ -173,8 +196,7 @@ def text_print():
if request.method == 'POST': if request.method == 'POST':
if session["text image path"] == None: if session["text image path"] == None:
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL) 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"]) return render_text_template()
image = format_image_to_label(get_file_path(session, session["text image path"])) image = format_image_to_label(get_file_path(session, session["text image path"]))
print_image(image) print_image(image)
@ -187,7 +209,7 @@ def text_print():
print("printing") print("printing")
session["cut"] = False; session["cut"] = False;
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) return render_text_template()
@app.route('/user_data/<filename>') @app.route('/user_data/<filename>')
@ -195,15 +217,6 @@ def user_data(filename):
return send_from_directory(get_folder_path(session), filename) return send_from_directory(get_folder_path(session), filename)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return redirect(url_for('static', filename='uploads/' + filename))
@app.route('/text/<filename>')
def generated_file(filename):
return redirect(url_for('static', filename='text/' + filename))
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=False) app.run(debug=False)

View file

@ -2,3 +2,5 @@
label_width = 213 label_width = 213
printer_width = 512 printer_width = 512
max_label_length = 2000

View file

@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from colorama import Fore, Style from colorama import Fore, Style
from printer_info import * from config import *
font = "resources/ComicMono.ttf" font = "resources/ComicMono.ttf"
@ -27,14 +27,24 @@ def gen_image(height, text1, text2):
print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2) print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2)
# the 1 here assures we don't get a 0 width image # the 1 here assures we don't get a 0 width image
img = Image.new('1', (max(text1_width, text2_width, 1), height), color=1) width = max(text1_width, text2_width, 1)
img = Image.new('1', (width, height), color=1)
draw = ImageDraw.Draw(img) draw = ImageDraw.Draw(img)
draw.text((0, text1["pos"]), text1["string"], font=font1) draw.text((0, text1["pos"]), text1["string"], font=font1)
draw.text((0, text2["pos"]), text2["string"], font=font2) draw.text((0, text2["pos"]), text2["string"], font=font2)
return img
if (width > max_label_length):
message = f"Label too long, max allowed length = {max_label_length}, yours = {width}."
status = "Error"
else:
message = None
status = None
return message, status, img
Text1 = { Text1 = {
"string": "Hello world", "string": "Hello world",
@ -48,4 +58,3 @@ Text2 = {
"pos": 40, "pos": 40,
} }
gen_image(label_width, Text1, Text2).save('output.png')

View file

@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
dev_path="/dev/usb/lp0" dev_path="/dev/usb/lp0"
from format_image import * from process_image import *
from PIL import Image from PIL import Image
import ctypes import ctypes
import io import io

View file

@ -1,6 +1,6 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from PIL import Image from PIL import Image
from printer_info import * from config import *
def format_image_to_label(path): def format_image_to_label(path):
image = Image.open(path) image = Image.open(path)
@ -11,10 +11,15 @@ def format_image_to_label(path):
return new_image return new_image
def format_image(path): def process_image(image_filestream):
image = Image.open(path) image = Image.open(image_filestream)
message = None
status = None
if (image.height != label_width): if (image.height != label_width):
message = f"image height = {image.height} pixels, will be resized to fit {label_width}"
status = "Info"
print("resizing image") print("resizing image")
new_height = label_width new_height = label_width
new_width = int(label_width * image.width / image.height) new_width = int(label_width * image.width / image.height)
@ -22,7 +27,12 @@ def format_image(path):
image = image.resize((new_width, new_height)) image = image.resize((new_width, new_height))
image = image.convert('1', dither=Image.FLOYDSTEINBERG) image = image.convert('1', dither=Image.FLOYDSTEINBERG)
return image
if image.width > max_label_length:
message= f"Label too long, max allowed length = {max_label_length}, yours = {image.width}."
status = "Error"
return message, status, image
#format_image("static/uploads/upload.png").save("output.png") #format_image("static/uploads/upload.png").save("output.png")

View file

@ -28,6 +28,10 @@
border: 2px solid #55cdfc; /* Light blue */ border: 2px solid #55cdfc; /* Light blue */
transition: background-color 0.3s ease; transition: background-color 0.3s ease;
} }
.info {
font-size: 20px;
}
.box:hover { .box:hover {
background-color: #f7a8b8; /* Softer pink on hover */ background-color: #f7a8b8; /* Softer pink on hover */

View file

@ -5,11 +5,15 @@
<h1>Upload Image</h1> <h1>Upload Image</h1>
<form method="POST" action="/image" 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="Generate preview"> <input type="submit" value="Generate preview"><br>
Recommended image height = {{label_width}}, taller images will be scaled
</form> </form>
{% if info %}
<p class="info" style="color: {{info_color}}">{{info}}</p>
{% endif %}
{% if filename %} {% if filename %}
<br><br>
<img src="user_data/{{filename}}" alt="Uploaded image" style="height: 150px; width: auto;"> <img src="user_data/{{filename}}" alt="Uploaded image" style="height: 150px; width: auto;">
<form method="POST" action="/image-print"> <form method="POST" action="/image-print">

View file

@ -32,8 +32,11 @@
<input type="submit" value="Generate preview"> <input type="submit" value="Generate preview">
</form> </form>
{% if info %}
<p class="info" style="color: {{info_color}}">{{info}}</p>
{% endif %}
{% if filename %} {% if filename %}
<br><br>
<img src="user_data/{{filename}}" alt="Generated Image" style="height: 150px; width: auto;"> <img src="user_data/{{filename}}" alt="Generated Image" style="height: 150px; width: auto;">
<form method="POST" action="/text-print"> <form method="POST" action="/text-print">
@ -48,5 +51,6 @@
</form> </form>
{% endif %} {% endif %}
{% endblock %} {% endblock %}