首页 > 解决方案 > 使用 Treepoem 和 BWIPP (Python) 将额外文本添加到条码

问题描述

我正在使用 treepoem(以及 BWIPP)生成条形码,我希望条形码的编号在下方,其他附加文本在上方。我试图将这一切保留在 eps 文件生成中,而不是以任何其他形式转换/编辑,因为这对于我在生成后对它们所做的事情很重要。

以下是我的代码(我尝试了文档中的各种不同选项,包括alttext,但均未成功)。我正在使用Python 3.8.2Treepoem 3.10.0

import treepoem # for barcode generation

def generate_rlb_barcode(barcode_type, barcode_data, image_name):

    eps_image = treepoem.generate_barcode(barcode_type, barcode_data, options={'includetext': True})
    print("image generation complete.")
    eps_image.convert('L')
    print("image converted to monochrome.")
    eps_image.save(image_name)
    print("image saved as " + str(image_name))

def main():
    barcode_image_name = 'test_img'
    generate_rlb_barcode('upca', '123456059973', (barcode_image_name + ".eps"))

main()

标签: pythonbarcodeghostscriptpostscript

解决方案


安装 treepoem 包:

pip install treepoem
# or
python -m pip install treepoem

运行此代码

import treepoem
image = treepoem.generate_barcode(
    barcode_type="code128",  # One of the BWIPP supported codes.
    data="Yout text-978316148fsd4100",
    options= {"includetext": True}
)
image.convert("1").save("output_qrcode_or_barcode.png")

output_qrcode_or_barcode.png


推荐阅读