label_printer/format_image.py

28 lines
710 B
Python
Raw Normal View History

2024-09-14 18:49:57 +00:00
from PIL import Image
2024-09-15 12:57:26 +00:00
from printer_info import *
2024-09-14 18:49:57 +00:00
2024-09-15 12:57:26 +00:00
def format_image_to_label(path):
2024-09-14 18:49:57 +00:00
image = Image.open(path)
image = image.rotate(-90, expand=True)
2024-09-15 12:57:26 +00:00
new_image = Image.new('1', (printer_width, image.height), 1)
2024-09-14 18:49:57 +00:00
new_image.paste(image, (0, 0))
return new_image
2024-09-15 12:57:26 +00:00
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")