Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
def generate_barcode(product_code):
    Code128 = barcode.get_barcode_class('code128')
    writer = ImageWriter()

    dpi = 300  # Use a fixed DPI value (adjust it according to your printer's DPI)
    page_size_mm = (25.4, 54)
    page_size_pixels = tuple(int(dpi * page_size_mm[i] / 25.4) for i in (0, 1))

    # Calculate the required module width and height based on the desired size
    desired_width = page_size_pixels[1] - 10  # Subtract a small margin
    desired_height = page_size_pixels[0] - 10  # Subtract a small margin

    writer.dpi = dpi
    writer.module_width = int(desired_width / 110)  # Adjust the value to fit the desired width
    writer.module_height = int(desired_height / 30)  # Adjust the value to fit the desired height
    writer.font_size = 12
    writer.text_distance = 0
    writer.quiet_zone = 6.5
    barcode_image = Code128(product_code, writer=writer)
    output_filename = 'temp_barcode'
    barcode_image.save(output_filename)

    # Rotate the image by 90 degrees
    image = Image.open(output_filename + ".png")
    rotated_image = image.rotate(270, expand=True)  # Rotate 270 degrees to achieve the desired orientation
    rotated_image.save(output_filename + ".png")

    return output_filename