跳转到主要内容

Python的PDF417 2D条码生成器

项目描述

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

使用PDF417格式轻松将您的数据编码为2D条码。

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

在MIT许可证下授权,请参阅LICENSE

安装

使用pip安装

pip install pdf417

命令行界面

可以使用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()

使用概述

from pdf417 import encode, render_image, render_svg

# Some data to encode
text = """Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated."""

# Convert to code words
codes = encode(text)

# Generate barcode as image
image = render_image(codes)  # Pillow Image object
image.save('barcode.jpg')

# Generate barcode as SVG
svg = render_svg(codes)  # ElementTree object
svg.write("barcode.svg")

支持字符串(py2中的unicode)和字节数组(py2中的str)

# These two inputs encode to the same code words
encode(u"love 💔")
encode(b"love \xf0\x9f\x92\x94")

# Default encoding is UTF-8, but you can specify your own
encode(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

该函数返回一个包含条码的Pillow 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

该函数返回一个包含SVG格式的条码的 ElementTree 对象。

render_image 不同,此函数不接受背景颜色选项。背景保持透明。

codes = encode(text, columns=3)
svg = render_svg(codes, scale=5, ratio=2, color="Seaweed")
svg.write('barcode.svg')

另请参阅

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源分发

pdf417-0.8.1.tar.gz (24.0 kB 查看哈希值)

上传时间

构建分发

pdf417-0.8.1-py2.py3-none-any.whl (23.7 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面