From d00ceef28281016007a5a82fa5e59cc1fcc4db6d Mon Sep 17 00:00:00 2001 From: zman Date: Tue, 8 Apr 2025 13:25:30 -0400 Subject: [PATCH] f --- app.py | 66 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/app.py b/app.py index 034042d..3ff935a 100644 --- a/app.py +++ b/app.py @@ -32,9 +32,15 @@ app.logger.setLevel(logging.DEBUG) # Create a queue to hold the files for processing file_queue = queue.Queue() +image_queue = queue.Queue() + +lock = threading.Lock() + # Directory to save uploaded files (you can modify this path) UPLOAD_FOLDER = './uploads' +IMAGE_FOLDER = './images' os.makedirs(UPLOAD_FOLDER, exist_ok=True) +os.makedirs(IMAGE_FOLDER, exist_ok=True) # Printer configuration printer_model = "QL-1100" @@ -45,7 +51,6 @@ printer = 'usb://0x04f9:0x20a7' def convert_pdf_to_images(pdf_path): """Converts a PDF to a PIL Image""" try: - # Convert PDF to image (get the first page) images = pdf2image.convert_from_path(pdf_path) if images: return images @@ -55,23 +60,19 @@ def convert_pdf_to_images(pdf_path): return None # Function to print address labels -def print_address_label(images, pdf_path): +def print_address_label(image, pdf_path): try: - processed_images = [] - for image in images: - if 'address' in pdf_path.lower(): - target_width = 1660 - target_height = 1164 - image = image.convert("RGB") - image = image.resize((target_width, target_height), Image.LANCZOS) - image = image.rotate(90, expand=True) - processed_images.append(image) - else: - target_width = 1164 - target_height = 1660 - image = image.convert("RGB") - image = image.resize((target_width, target_height), Image.LANCZOS) - processed_images.append(image) + if 'address' in pdf_path.lower(): + target_width = 1660 + target_height = 1164 + image = image.convert("RGB") + image = image.resize((target_width, target_height), Image.LANCZOS) + image = image.rotate(90, expand=True) + else: + target_width = 1164 + target_height = 1660 + image = image.convert("RGB") + image = image.resize((target_width, target_height), Image.LANCZOS) qlr = BrotherQLRaster(printer_model) qlr.exception_on_warning = True @@ -79,7 +80,7 @@ def print_address_label(images, pdf_path): app.logger.info("Converting image to printer instructions...") instructions = convert( qlr=qlr, - images=processed_images, + images=[image], label='102x152', threshold=70.0, dither=False, @@ -103,8 +104,19 @@ def print_address_label(images, pdf_path): except Exception as e: app.logger.error(f"Error during printing: {str(e)}") +def save_images(images, pdf_path): + uuid = str(int(time.time())) + for i,image in enumerate(images): + if 'address' in pdf_path.lower(): + image.save(os.path.join(IMAGE_FOLDER, f"address_label_{uuid}_{i}.png"), "PNG") + elif 'packing' in pdf_path.lower(): + image.save(os.path.join(IMAGE_FOLDER, f"shipping_label_{uuid}_{i}.png"), "PNG") + else: + image.save(os.path.join(IMAGE_FOLDER, f"unknown_{uuid}_{i}.png"), "PNG") + image_queue.put((image, pdf_path)) + # Worker thread that processes files from the queue -def process_queue(): +def file_worker(): while True: # Wait until a file is available in the queue file_path = file_queue.get() @@ -114,15 +126,25 @@ def process_queue(): app.logger.info(f"Processing file: {file_path}") images = convert_pdf_to_images(file_path) if images: - print_address_label(images, file_path) + save_images(images, file_path) else: app.logger.error(f"Failed to convert PDF to images: {file_path}") # Signal that the task is done file_queue.task_done() +def printer_worker(): + while True: + image, path = image_queue.get() + if image is None: + break + with lock: + print_address_label(image, path) + image_queue.task_done() + # Start the worker thread -thread = threading.Thread(target=process_queue, daemon=True) -thread.start() +threading.Thread(target=file_worker, daemon=True).start() +# Start the printer worker thread +threading.Thread(target=printer_worker, daemon=True).start() @app.route('/upload', methods=['POST']) def upload_file():