Compare commits

3 Commits
main ... daemon

Author SHA1 Message Date
34733206c2 Font size support 2026-02-19 18:47:16 -03:00
052723bdbc Initial Dockerfile 2026-02-09 01:09:06 -03:00
3a06ccbb91 Now works directly with blehd socket 2026-02-09 00:34:34 -03:00
5 changed files with 287 additions and 81 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.venv
__pycache__
bleh

29
Dockerfile Normal file
View File

@@ -0,0 +1,29 @@
FROM python:3.14-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1
WORKDIR /app
# Optional but useful for Pillow runtime compatibility in slim images
RUN apt-get update && apt-get install -y --no-install-recommends \
libjpeg62-turbo \
zlib1g \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
# Non-root runtime user
RUN useradd -m -u 10001 catnote && chown -R catnote:catnote /app
USER catnote
EXPOSE 5000
# BLEHD_SOCKET can be overridden at runtime
ENV BLEHD_SOCKET=/run/bleh/blehd.sock
CMD ["python", "app.py", "--port", "5000"]

313
app.py
View File

@@ -1,8 +1,8 @@
import io
import tempfile
import subprocess
import requests
import base64
import socket
from flask import Flask, render_template, request, send_from_directory, session
from PIL import Image, ImageDraw, ImageFont
import os
@@ -17,17 +17,202 @@ FONT_PATH = "./fonts/ubuntu/Ubuntu-Regular.ttf"
FONT_PATH_BOLD = "./fonts/ubuntu/Ubuntu-Bold.ttf"
FONT_PATH_OBLIQUE = "./fonts/ubuntu/Ubuntu-Italic.ttf"
FONT_PATH_BOLDITALIC = "./fonts/ubuntu/Ubuntu-BoldItalic.ttf"
FONT_SIZE = 24
HEADER_SIZE_1 = 56
HEADER_SIZE_2 = 34
HEADER_SIZE_3 = 28
DEFAULT_FONT_SIZE = 24
FONT_SIZE_OPTIONS = {
"small": 18,
"normal": 24,
"large": 30,
"xlarge": 36,
}
HEADER_RATIO_1 = 56 / 24
HEADER_RATIO_2 = 34 / 24
HEADER_RATIO_3 = 28 / 24
BANNER_FONT_SIZE = 300
IMAGE_WIDTH = 384
BULLET_CHAR = ""
MIN_LINES = 86
BLEHD_SOCKET = os.environ.get("BLEHD_SOCKET", "/run/bleh/blehd.sock")
with open('static/translations.json', 'r', encoding='utf-8') as f:
TRANSLATIONS = json.load(f)
def _dither_none(gray_img, levels):
if levels == 2:
return gray_img.point(lambda p: 0 if p < 128 else 255, mode="L")
step = 255 / (levels - 1)
return gray_img.point(lambda p: int(round(p / step) * step), mode="L")
def _dither_ordered(gray_img, matrix, levels, strength=1.0):
src = gray_img.load()
w, h = gray_img.size
out = Image.new("L", (w, h), 255)
dst = out.load()
n = len(matrix)
step = 255 / (levels - 1)
for y in range(h):
for x in range(w):
t = (matrix[y % n][x % n] / (n * n) - 0.5) * step * strength
v = max(0, min(255, int(src[x, y] + t)))
q = int(round(v / step) * step)
dst[x, y] = q
return out
def _dither_error_diffusion(gray_img, levels, matrix, divisor):
w, h = gray_img.size
buf = [[float(gray_img.getpixel((x, y))) for x in range(w)] for y in range(h)]
step = 255 / (levels - 1)
for y in range(h):
for x in range(w):
old = buf[y][x]
new = round(old / step) * step
new = max(0, min(255, new))
err = old - new
buf[y][x] = new
for dx, dy, weight in matrix:
nx = x + dx
ny = y + dy
if 0 <= nx < w and 0 <= ny < h:
buf[ny][nx] += err * (weight / divisor)
out = Image.new("L", (w, h), 255)
for y in range(h):
for x in range(w):
out.putpixel((x, y), int(max(0, min(255, round(buf[y][x])))))
return out
def _build_bayer(n):
m = [[0, 2], [3, 1]]
size = 2
while size < n:
prev = m
size *= 2
m = [[0] * size for _ in range(size)]
for y in range(size // 2):
for x in range(size // 2):
v = prev[y][x] * 4
m[y][x] = v + 0
m[y][x + size // 2] = v + 2
m[y + size // 2][x] = v + 3
m[y + size // 2][x + size // 2] = v + 1
return m
def apply_dither(gray_img, dither_type, levels):
if dither_type == "none":
return _dither_none(gray_img, levels)
if dither_type == "floyd":
# x+1,y ; x-1,y+1 ; x,y+1 ; x+1,y+1
fs = [(1, 0, 7), (-1, 1, 3), (0, 1, 5), (1, 1, 1)]
return _dither_error_diffusion(gray_img, levels, fs, 16)
if dither_type == "atkinson":
at = [(1, 0, 1), (2, 0, 1), (-1, 1, 1), (0, 1, 1), (1, 1, 1), (0, 2, 1)]
return _dither_error_diffusion(gray_img, levels, at, 8)
if dither_type == "jjn":
# Jarvis-Judice-Ninke kernel centered at current pixel
jjn = [
(1, 0, 7), (2, 0, 5),
(-2, 1, 3), (-1, 1, 5), (0, 1, 7), (1, 1, 5), (2, 1, 3),
(-2, 2, 1), (-1, 2, 3), (0, 2, 5), (1, 2, 3), (2, 2, 1),
]
return _dither_error_diffusion(gray_img, levels, jjn, 48)
if dither_type == "bayer2x2":
return _dither_ordered(gray_img, _build_bayer(2), levels, strength=1.0 if levels == 2 else 0.2)
if dither_type == "bayer4x4":
return _dither_ordered(gray_img, _build_bayer(4), levels, strength=1.0 if levels == 2 else 0.2)
if dither_type == "bayer8x8":
return _dither_ordered(gray_img, _build_bayer(8), levels, strength=1.0 if levels == 2 else 0.2)
if dither_type == "bayer16x16":
return _dither_ordered(gray_img, _build_bayer(16), levels, strength=1.0 if levels == 2 else 0.2)
return _dither_none(gray_img, levels)
def preprocess_for_mode(img, dithering, mode):
img = remove_alpha(img)
if img.width <= 0 or img.height <= 0:
raise ValueError("Invalid image dimensions")
ratio = img.width / img.height
height = int(IMAGE_WIDTH / ratio)
if height <= 0:
raise ValueError("Computed invalid image height")
img = img.resize((IMAGE_WIDTH, height), Image.Resampling.LANCZOS).convert("L")
levels = 2 if mode == "1bpp" else 16
out = apply_dither(img, dithering, levels)
return out
def pack_pixels(img, mode):
width, height = img.size
if width != IMAGE_WIDTH:
img = img.resize((IMAGE_WIDTH, height), Image.Resampling.LANCZOS)
width = IMAGE_WIDTH
if mode == "1bpp":
packed = bytearray((width * height) // 8)
px = img.load()
for y in range(height):
for x in range(width):
if px[x, y] < 128:
idx = (y * width + x) // 8
packed[idx] |= 1 << (x % 8)
return bytes(packed), height
# 4bpp
packed = bytearray((width * height) // 2)
px = img.load()
for y in range(height):
for x in range(width):
gray = px[x, y]
level = (255 - gray) >> 4
idx = (y * width + x) >> 1
shift = ((x & 1) ^ 1) << 2
packed[idx] |= (level & 0x0F) << shift
return bytes(packed), height
def image_from_url(url, dithering, mode):
resp = requests.get(url, stream=True, timeout=20)
resp.raise_for_status()
img = Image.open(io.BytesIO(resp.content))
return preprocess_for_mode(img, dithering, mode)
def image_from_bytes(image_bytes, dithering, mode):
img = Image.open(io.BytesIO(image_bytes))
return preprocess_for_mode(img, dithering, mode)
def pad_image_to_min_lines(img):
if img.height >= MIN_LINES:
return img
out = Image.new("L", (img.width, MIN_LINES), 255)
out.paste(img, (0, 0))
return out
def blehd_call(method, params=None, socket_path=BLEHD_SOCKET, timeout=90):
req = {"id": "1", "method": method, "params": params or {}}
data = (json.dumps(req, ensure_ascii=False) + "\n").encode("utf-8")
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s:
s.settimeout(timeout)
s.connect(socket_path)
s.sendall(data)
f = s.makefile("rb")
line = f.readline()
if not line:
raise RuntimeError("No response from blehd")
resp = json.loads(line.decode("utf-8"))
if not resp.get("ok"):
err = resp.get("error") or {}
raise RuntimeError(err.get("message", "Unknown blehd error"))
return resp.get("result")
def remove_uploaded_img():
path = session.pop('uploaded_img_path', None)
if path and os.path.exists(path):
@@ -50,45 +235,11 @@ def remove_alpha(img):
return img
def bleh_image_from_url(url, dithering, mode):
resp = requests.get(url, stream=True)
resp.raise_for_status()
img = Image.open(io.BytesIO(resp.content))
img = remove_alpha(img)
buf = io.BytesIO()
img.save(buf, format="PNG")
image_bytes_no_alpha = buf.getvalue()
bleh = subprocess.Popen(
["./bleh", "-o", "-", "-mode", f"{mode}", "-d", f"{dithering}", "-"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = bleh.communicate(image_bytes_no_alpha)
if bleh.returncode != 0:
raise RuntimeError(f"Driver failed: {err.decode()}")
img = Image.open(io.BytesIO(out)).convert("L")
# Optionally check width, pad/resize if needed
if img.width != IMAGE_WIDTH:
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
return img
return image_from_url(url, dithering, mode)
def bleh_image_from_bytes(image_bytes, dithering, mode):
# OPEN the uploaded image and remove alpha
img = Image.open(io.BytesIO(image_bytes))
img = remove_alpha(img)
buf = io.BytesIO()
img.save(buf, format="PNG")
image_bytes_no_alpha = buf.getvalue()
# pass that to bleh
bleh = subprocess.Popen(
["./bleh", "-o", "-", "-mode", f"{mode}", "-d", f"{dithering}", "-"],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = bleh.communicate(image_bytes_no_alpha)
if bleh.returncode != 0:
raise RuntimeError(f"Driver failed: {err.decode()}")
img = Image.open(io.BytesIO(out)).convert("L")
if img.width != IMAGE_WIDTH:
img = img.resize((IMAGE_WIDTH, img.height), Image.LANCZOS)
return img
return image_from_bytes(image_bytes, dithering, mode)
def rotate_image_bytes(image_bytes, rotation):
if rotation == 0:
@@ -198,18 +349,24 @@ def wrap_segments(segments, font, font_bold, font_italic, font_bolditalic, max_w
if line:
yield line
def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
font_bold = ImageFont.truetype(FONT_PATH_BOLD, FONT_SIZE)
font_italic = ImageFont.truetype(FONT_PATH_OBLIQUE, FONT_SIZE)
def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False, font_size=None):
if font_size is None:
font_size = DEFAULT_FONT_SIZE
header_size_1 = round(font_size * HEADER_RATIO_1)
header_size_2 = round(font_size * HEADER_RATIO_2)
header_size_3 = round(font_size * HEADER_RATIO_3)
font = ImageFont.truetype(FONT_PATH, font_size)
font_bold = ImageFont.truetype(FONT_PATH_BOLD, font_size)
font_italic = ImageFont.truetype(FONT_PATH_OBLIQUE, font_size)
font_banner = ImageFont.truetype(FONT_PATH_BOLD, BANNER_FONT_SIZE)
try:
font_bolditalic = ImageFont.truetype(FONT_PATH_BOLDITALIC, FONT_SIZE)
font_bolditalic = ImageFont.truetype(FONT_PATH_BOLDITALIC, font_size)
except:
font_bolditalic = None
font_h1 = ImageFont.truetype(FONT_PATH_BOLD, HEADER_SIZE_1)
font_h2 = ImageFont.truetype(FONT_PATH_BOLD, HEADER_SIZE_2)
font_h3 = ImageFont.truetype(FONT_PATH_BOLD, HEADER_SIZE_3)
font_h1 = ImageFont.truetype(FONT_PATH_BOLD, header_size_1)
font_h2 = ImageFont.truetype(FONT_PATH_BOLD, header_size_2)
font_h3 = ImageFont.truetype(FONT_PATH_BOLD, header_size_3)
if bannermode:
# Remove line breaks for single-line banner
@@ -247,17 +404,17 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
image = bleh_image_from_url(tag[1], dithering, printmode)
lines_out.append(('image', image))
except Exception as e:
lines_out.append(('text', [('text', f"[Imagen inválida: {e}]")], font, FONT_SIZE))
lines_out.append(('text', [('text', f"[Imagen inválida: {e}]")], font, font_size))
elif tag[0] == 'userimage':
if uploaded_img_bytes:
try:
image = bleh_image_from_bytes(uploaded_img_bytes, dithering, printmode)
lines_out.append(('image', image))
except Exception as e:
lines_out.append(('text', [('text', f"[Error al procesar imagen]")], font, FONT_SIZE))
lines_out.append(('text', [('text', f"[Error al procesar imagen]")], font, font_size))
print(f"Image processing error: {e}")
else:
lines_out.append(('text', [('text', "[No se subió una imagen]")], font, FONT_SIZE))
lines_out.append(('text', [('text', "[No se subió una imagen]")], font, font_size))
elif tag[0] == 'hr':
lines_out.append(('hr',))
elif tag[0] == 'header':
@@ -265,13 +422,13 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
segments = tag[2]
if header_level == 1:
font_h = font_h1
size_h = HEADER_SIZE_1
size_h = header_size_1
elif header_level == 2:
font_h = font_h2
size_h = HEADER_SIZE_2
size_h = header_size_2
else:
font_h = font_h3
size_h = HEADER_SIZE_3
size_h = header_size_3
for wrapped in wrap_segments(segments, font_h, font_h, font_h, font_h, IMAGE_WIDTH):
lines_out.append(('header', wrapped, font_h, size_h))
elif tag[0] == 'bullet':
@@ -281,9 +438,9 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
wrapped_lines = list(wrap_segments(segments, font, font_bold, font_italic, font_bolditalic, IMAGE_WIDTH - bullet_w, start_x=0))
for i, wrapped in enumerate(wrapped_lines):
if i == 0:
lines_out.append(('bullet', wrapped, bullet_font, FONT_SIZE, True, bullet_w))
lines_out.append(('bullet', wrapped, bullet_font, font_size, True, bullet_w))
else:
lines_out.append(('bullet', wrapped, bullet_font, FONT_SIZE, False, bullet_w))
lines_out.append(('bullet', wrapped, bullet_font, font_size, False, bullet_w))
elif tag[0] == 'ordered':
idx, segments = tag[1], tag[2]
num_str = f"{idx}. "
@@ -292,13 +449,13 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
wrapped_lines = list(wrap_segments(segments, font, font_bold, font_italic, font_bolditalic, IMAGE_WIDTH - num_w, start_x=0))
for i, wrapped in enumerate(wrapped_lines):
if i == 0:
lines_out.append(('ordered', wrapped, number_font, FONT_SIZE, num_str, True, num_w))
lines_out.append(('ordered', wrapped, number_font, font_size, num_str, True, num_w))
else:
lines_out.append(('ordered', wrapped, number_font, FONT_SIZE, num_str, False, num_w))
lines_out.append(('ordered', wrapped, number_font, font_size, num_str, False, num_w))
else: # normal text
segments = tag[1]
for wrapped in wrap_segments(segments, font, font_bold, font_italic, font_bolditalic, IMAGE_WIDTH):
lines_out.append(('text', wrapped, font, FONT_SIZE))
lines_out.append(('text', wrapped, font, font_size))
# Compute total height, including images
height = 10 # Top margin
@@ -308,7 +465,7 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
elif item[0] == 'hr':
height += 10
elif item[0] == 'blank':
height += FONT_SIZE
height += font_size
elif item[0] == 'image':
img = item[1]
height += img.height + 10 # add margin below image
@@ -318,7 +475,7 @@ def render(md, dithering, printmode, uploaded_img_bytes=None, bannermode=False):
y = 0
for item in lines_out:
if item[0] == 'blank':
y += FONT_SIZE
y += font_size
elif item[0] == 'hr':
draw.line((0, y + 5, IMAGE_WIDTH, y + 5), fill=0, width=2)
y += 10
@@ -427,11 +584,14 @@ def index():
dithering = request.form.get("dithering", "floyd")
printmode = request.form.get("printmode", "1bpp")
bannermode = bool(request.form.get("bannermode"))
image = render(md, dithering, printmode, uploaded_img_bytes, bannermode=bannermode)
fontsize_key = request.form.get("fontsize", "normal")
font_size = FONT_SIZE_OPTIONS.get(fontsize_key, DEFAULT_FONT_SIZE)
image = render(md, dithering, printmode, uploaded_img_bytes, bannermode=bannermode, font_size=font_size)
session['dithering'] = dithering
session['printmode'] = printmode
session['rotation'] = rotation
session['bannermode'] = bannermode
session['fontsize'] = fontsize_key
buf = io.BytesIO()
image.save(buf, format="PNG")
buf.seek(0)
@@ -445,25 +605,21 @@ def index():
error = "Intensity must be a number between 0 and 100"
intensity = 85
session['intensity'] = intensity
# If print button pressed, send to driver
# If print button pressed, send to blehd directly via Unix socket
if "print" in request.form:
try:
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmpfile:
image.save(tmpfile, format="PNG")
tmpfile.flush()
# Run the bleh command
result = subprocess.run([
"./bleh",
"-mode", f"{printmode}",
"-intensity", f"{intensity}",
tmpfile.name
], capture_output=True, text=True, timeout=90)
if result.returncode != 0:
error = f"Printer error: {result.stderr or result.stdout}"
else:
printable = pad_image_to_min_lines(image)
pixels, height = pack_pixels(printable, printmode)
params = {
"mode": printmode,
"intensity": intensity,
"height": height,
"pixels_b64": base64.b64encode(pixels).decode("ascii"),
}
blehd_call("print.start", params)
printed = True
except Exception as e:
error = f"Failed to print: {e}"
error = f"Failed to print via blehd socket: {e}"
return render_template(
"index.html",
dithering_modes=dithering_modes,
@@ -476,6 +632,7 @@ def index():
current_rotation=session.get('rotation', 0),
current_printmode=session.get('printmode', '1bpp'),
current_bannermode=session.get('bannermode', False),
current_fontsize=session.get('fontsize', 'normal'),
t=t,
lang=lang
)

View File

@@ -33,7 +33,12 @@
"image_url": "Image URL",
"leave_an_empty_line": "Leave an empty line",
"uses_the_uploaded_image": "(uses the uploaded image)",
"no_dithering": "No dithering"
"no_dithering": "No dithering",
"font_size": "Font size:",
"font_small": "Small",
"font_normal": "Normal",
"font_large": "Large",
"font_xlarge": "Extra Large"
},
"es": {
"catnote": "CatNote",
@@ -69,6 +74,11 @@
"image_url": "URL de la imagen",
"leave_an_empty_line": "Deje una línea vacía",
"uses_the_uploaded_image": "(usa la imagen cargada)",
"no_dithering": "Sin dithering"
"no_dithering": "Sin dithering",
"font_size": "Tamaño de fuente:",
"font_small": "Pequeño",
"font_normal": "Normal",
"font_large": "Grande",
"font_xlarge": "Extra grande"
}
}

View File

@@ -571,6 +571,15 @@
<option value="270" {% if 270==current_rotation %}selected="selected" {% endif %}>270°</option>
</select>
</div>
<div class="options-fontsize">
<label for="fontsize">{{ t['font_size'] }}</label>
<select name="fontsize">
<option value="small" {% if "small"==current_fontsize %}selected="selected" {% endif %}>{{ t['font_small'] }}</option>
<option value="normal" {% if "normal"==current_fontsize %}selected="selected" {% endif %}>{{ t['font_normal'] }}</option>
<option value="large" {% if "large"==current_fontsize %}selected="selected" {% endif %}>{{ t['font_large'] }}</option>
<option value="xlarge" {% if "xlarge"==current_fontsize %}selected="selected" {% endif %}>{{ t['font_xlarge'] }}</option>
</select>
</div>
<div class="options-printmode">
<label for="printmode">{{ t['print_mode'] }}</label>
<select name="printmode">