PDF417 2D Barcode Generator for Python

pdf417的Python项目详细描述


https://img.shields.io/travis/mosquito/pdf417.svg?maxAge=3600&style=flat-squarehttps://img.shields.io/badge/author-%40mosquito-blue.svg?maxAge=3600&style=flat-squarehttps://img.shields.io/github/license/mosquito/pdf417.svg?maxAge=3600&style=flat-squarehttps://img.shields.io/pypi/v/pdf417.svg?maxAge=3600&style=flat-square

使用PDF417格式将数据轻松编码为二维条码。

https://raw.githubusercontent.com/mosquito/pdf417/master/images/1_basic.jpg

根据麻省理工学院的许可,请参阅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()
https://raw.githubusercontent.com/mosquito/pdf417/master/images/2_columns.jpg

安全级别

提高安全级别将产生更强(和更多)的错误 纠错码,使条码更大,但不容易腐败。这个 安全级别可以从0到8,并且会导致2^(level+1)错误 修正码字,意思是0级产生2个码字,8级 产生512。默认安全级别为2。

codes=encode(text,columns=12,security_level=6)image=render_image(codes)image.show()
https://raw.githubusercontent.com/mosquito/pdf417/master/images/3_security_level.jpg

自动数字压缩模式

此模式可以将几乎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()
https://raw.githubusercontent.com/mosquito/pdf417/master/images/4_rendering.jpg

渲染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')

另见

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
javaelk:如何在Kibana中按异常类对stacktrace进行分组   java两个组件使用MigLayout相互重叠   java Hibernate标准获取关联实体的列表,而不是父实体的列表   从Java关闭另一个应用程序,但不是由当前Java应用程序启动   java使用OGNL获取参数   java如何在SSLEngine中启用密码TLS\U DHE\U RSA\U和\U AES\U 256\U GCM\U SHA384   在Java中有效地比较两个列表<Object[]>   java杀死挂起的线程   在java中从指定模式前后的字符串中提取子字符串   存储整数的java HashMap替代方案   java如何使用LibGDX加载特定于语言的资产?   java如何使用JSON响应从维基百科读取结构化数据   java无法连接到Spark Master:原因是:[已解除关联]   java如何配置Elastic beanstalk classic负载平衡器以使用CLI终止HTTPS   java筛网中的奥斯汀大于int   java PircBot在每个用户上迭代一组命令   java将带有Jackson的hashmap编组为XML的错误结构   testng中的java设置testfailure给出了正回报   java如何在IntelliJ中正确配置Eclipse项目?