PDF417 2D Barcode Generator for Python
pdf417的Python项目详细描述
使用PDF417格式将数据轻松编码为二维条码。
根据麻省理工学院的许可,请参阅LICENSE。
安装
使用pip安装:
pip install pdf417
cli
pdf417gen命令可用于从命令行生成条形码。它 将输入作为参数或从stdin获取。
# Show help pdf417gen encode --help # Encode given text and display the barcode pdf417gen encode "Beautiful is better than ugly"# Encode given text and save barcode to a file (extension determines format) pdf417gen encode -o barcode.png "Explicit is better than implicit"# Input from a file pdf417gen encode < input.txt # Piped input python -c "import this"| pdf417gen encode
用法
创建条形码分为两步:
- 使用encode()
- 使用下列呈现函数之一呈现条形码:render_image(), render_svg()。
使用概述:
frompdf417importencode,render_image,render_svg# Some data to encodetext="""Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated."""# Convert to code wordscodes=encode(text)# Generate barcode as imageimage=render_image(codes)# Pillow Image objectimage.save('barcode.jpg')# Generate barcode as SVGsvg=render_svg(codes)# ElementTree objectsvg.write("barcode.svg")
支持字符串(py2中的unicode)和字节数组(py2中的str):
# These two inputs encode to the same code wordsencode(u"love ?")encode(b"love \xf0\x9f\x92\x94")# Default encoding is UTF-8, but you can specify your ownencode(u"love ?",encoding="utf-8")
编码数据
第一步是将数据编码成一个码字列表。
encode(data,columns=6,security_level=2)
列
可以通过定义用于 呈现1到30之间的数据,默认值为6。条形码可以有 最大的90行,所以对于更大的数据集,您可能需要增加数量 以减少行数。
codes=encode(text,columns=12)image=render_image(codes)image.show()
安全级别
提高安全级别将产生更强(和更多)的错误 纠错码,使条码更大,但不容易腐败。这个 安全级别可以从0到8,并且会导致2^(level+1)错误 修正码字,意思是0级产生2个码字,8级 产生512。默认安全级别为2。
codes=encode(text,columns=12,security_level=6)image=render_image(codes)image.show()
自动数字压缩模式
此模式可以将几乎3位(2.93)信息打包为一个符号字符。 长度小于13个符号的单词将作为文本调用
codes=encode(text,numeric_compaction=True)
渲染图像
render_image函数接受以下选项:
- scale-模块宽度,以像素为单位(默认值:3)
- ratio-模块高宽比(默认值:3)
- padding-图像填充,以像素为单位(默认值:20)
- fg_color-前景色(默认值:#000000)
- bg_color-背景色(默认值:#FFFFFF)
注意
模块是条形码的最小元素,类似于像素。模块 在PDF417条码是高和窄。
函数返回包含条形码的枕头Image对象。
颜色可以指定为十六进制代码或使用HTML颜色名称。
codes=encode(text,columns=3)image=render_image(codes,scale=5,ratio=2,padding=5,fg_color="Indigo",bg_color="#ddd")image.show()
渲染SVG
render_svg函数接受以下选项:
- scale-模块宽度,以像素为单位(默认值:3)
- ratio-模块高宽比(默认值:3)
- padding-图像填充,以像素为单位(默认值:20)
- color-前景色(默认值:\000000)
函数返回一个ElementTree对象,该对象包含svg格式的条形码。
与render_image不同,此函数不采用背景色选项。 背景保持透明。
codes=encode(text,columns=3)svg=render_svg(codes,scale=5,ratio=2,color="Seaweed")svg.write('barcode.svg')
另见
- pdf417-php-php实现
- golang-pdf417-a go实现
- Specifications-uss-pdf-417