label_printer/process_image.py

38 lines
1.1 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
from PIL import Image
from config import *
def format_image_to_label(path):
image = Image.open(path)
image = image.rotate(-90, expand=True)
new_image = Image.new('1', (printer_width, image.height), 1)
new_image.paste(image, (0, 0))
return new_image
def process_image(image_filestream):
image = Image.open(image_filestream)
message = None
status = None
if (image.height != label_width):
message = f"image height = {image.height} pixels, will be resized to fit {label_width}"
status = "Info"
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)
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")