test_ean13.py 726 B

123456789101112131415161718192021222324
  1. from barcode import EAN13
  2. from barcode.writer import ImageWriter
  3. import time
  4. # 创建条形码对象
  5. barcode = EAN13('123456789012', writer=ImageWriter())
  6. # 自定义绘制文本的方法
  7. def custom_text(text, width, height):
  8. # 计算文本的宽度和高度
  9. text_width, text_height = barcode.writer.font.getsize(text)
  10. # 计算文本的位置(使其靠上对齐)
  11. x = (width - text_width) / 2
  12. y = 0
  13. # 绘制文本
  14. barcode.writer._draw_text((x, y), text)
  15. # 替换默认的绘制文本方法
  16. barcode.writer._draw_text = custom_text
  17. # 保存生成的条形码图像文件
  18. barcode.save('/Users/jack/source/mySpace/mycode/temp_code/test_barcode_ean13/{}'.format(str(int(time.time()))))