Untitled
unknown
plain_text
3 years ago
1.5 kB
14
Indexable
def generate_custom_code128(product_code, dpi=203, paper_size=(1, 2.125)):
basethickness = 8
baseheight = 0.6
barcode = code128.image(product_code, thickness=basethickness, height=int(baseheight * dpi))
paper_width = int(paper_size[0] * dpi)
paper_height = int(paper_size[1] * dpi)
image = Image.new('1', (paper_width, paper_height), 1)
barcode = barcode.rotate(90, expand=True)
barcode_width, barcode_height = barcode.size
while (barcode_height > paper_height):
basethickness = basethickness - 1
barcode = code128.image(product_code, thickness=basethickness, height=int(baseheight * dpi))
barcode = barcode.rotate(90, expand=True)
barcode_width, barcode_height = barcode.size
x_pos = int((paper_width - barcode_width) / 2) - 40
y_pos = int((paper_height - barcode_height) / 2)
image.paste(barcode, (x_pos, y_pos))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", 50)
text_width, text_height = draw.textsize(product_code, font=font)
text_x = int((paper_width - text_width) / 2)
text_y = y_pos - text_height - 60
rotated_text = Image.new('1', (text_width, text_height), 1)
text_draw = ImageDraw.Draw(rotated_text)
text_draw.text((0, 0), product_code, font=font, fill=0)
rotated_text = rotated_text.rotate(90, expand=True)
image.paste(rotated_text, (text_x, text_y))
output = io.BytesIO()
image.save(output, format="PNG")
output.seek(0)
return output
Editor is loading...