Untitled
unknown
plain_text
a year ago
1.6 kB
3
Indexable
Never
def generate_barcode(product_code): Code128 = barcode.get_barcode_class('code128') barcode_image = Code128(product_code, writer=ImageWriter()) output_filename = 'temp_barcode' barcode_image.save(output_filename) return output_filename def print_barcode(printer, barcode_image): # Get the printer DC printer_dc = win32ui.CreateDC() printer_dc.CreatePrinterDC(printer) # Get the page size and image size in pixels page_size_mm = (25.4, 54) dpi = 300 page_size_pixels = tuple(int(dpi * page_size_mm[i] / 25.4) for i in (0, 1)) image = Image.open(barcode_image + ".png").convert('L') # Enhance the image quality image = image.resize((image.width * 4, image.height * 4), Image.LANCZOS) # Rotate the image by 90 degrees image = image.rotate(90, expand=True) # Set the new size directly based on the paper dimensions new_size = (300, int(300 * 2.125)) # Resize the image using the thumbnail method image.thumbnail(new_size, Image.LANCZOS) resized_width, resized_height = image.size # Calculate the position to center the image on the page x_pos = int((page_size_pixels[0] - resized_width) / 2) y_pos = int((page_size_pixels[1] - resized_height) / 2) - 80 # Start the print job printer_dc.StartDoc(barcode_image + ".png") printer_dc.StartPage() # Print the image dib = ImageWin.Dib(image) dib.draw(printer_dc.GetSafeHdc(), (x_pos, y_pos, resized_width, resized_height)) # End the print job printer_dc.EndPage() printer_dc.EndDoc() printer_dc.DeleteDC()