Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
Never
from PIL import Image

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))
    
    writer.dpi = dpi
    writer.module_width = int(page_size_pixels[0] / 110)  # Adjust the value to fit the width
    writer.module_height = int(page_size_pixels[1] / 2.125)  # Adjust the value to fit the 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