Untitled
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
def generate_custom_code128(product_code, dpi=300, paper_size=(1, 2.125)): barcode = code128.image(product_code, thickness=5, height=int(0.5 * 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 x_pos = int((paper_width - barcode_width) / 2) y_pos = int((paper_height - barcode_height) / 2) image.paste(barcode, (x_pos, y_pos)) draw = ImageDraw.Draw(image) font = ImageFont.truetype("arial.ttf", 10) text_width, text_height = draw.textsize(product_code, font=font) text_x = int((paper_width - text_width) / 2) text_y = y_pos - text_height - 2 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...