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