从Python 2和3读取和写入Data Matrix条形码。
项目描述
使用libdmtx库从Python 2和3读取和写入Data Matrix条形码。
纯Python
与PIL / Pillow图像、OpenCV / imageio / numpy ndarray和原始字节数据一起使用
解码条形码位置
除了libdmtx库本身外,没有其他依赖项
已在Python 2.7和Python 3.5到3.10上进行了测试
较老的pydmtx软件包卡在了Python 2.x领域。
安装
Windows Python轮盘中包含了libdmtx DLL。在其他操作系统上,您需要安装libdmtx共享库。
Mac OS X
brew install libdmtx
Linux
sudo apt-get install libdmtx0a
安装此Python包装器;使用第二种形式安装read_datamatrix和write_datamatrix命令行脚本的依赖项
pip install pylibdmtx pip install pylibdmtx[scripts]
示例用法
decode函数接受PIL.Image实例。
>>> from pylibdmtx.pylibdmtx import decode >>> from PIL import Image >>> decode(Image.open('pylibdmtx/tests/datamatrix.png')) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
它还接受可能来自使用OpenCV加载图像的numpy.ndarray实例。
>>> import cv2 >>> decode(cv2.imread('pylibdmtx/tests/datamatrix.png')) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
您还可以提供一个元组(pixels, width, height)
>>> image = cv2.imread('pylibdmtx/tests/datamatrix.png') >>> height, width = image.shape[:2] >>> decode((image.tobytes(), width, height)) [Decoded(data='Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data='Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]
encode函数生成包含Data Matrix条形码的图像
>>> from pylibdmtx.pylibdmtx import encode >>> encoded = encode('hello world'.encode('utf8')) >>> img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels) >>> img.save('dmtx.png') >>> print(decode(Image.open('dmtx.png'))) [Decoded(data=b'hello world', rect=Rect(left=9, top=10, width=80, height=79))]
Windows错误消息
如果在Windows上导入 pylibdmtx 时出现难看的 ImportError,您可能需要 Visual Studio 2013 的 Visual C++ 分发包。如果您使用的是64位Python,请安装 vcredist_x64.exe;如果您使用的是32位Python,请安装 vcredist_x86.exe。
限制
欢迎提交PR来解决这些问题。
我采取了复制 pydmtx 的 decode 函数逻辑(在 pydmtxmodule.c 中)的笨拙方法;可能有更多 libdmtx 的功能可以被有用地暴露出来
我公开了最少量的函数、定义、枚举和类型定义,以便重新实现 pydmtx 的 decode 函数
贡献者
Vinicius Kursancew (@kursancew) - 首次实现条码写入
Joseph Weston (@jbweston) - 支持 libdmtx 0.7.5
许可证
pylibdmtx 在 MIT 许可证下分发(见 LICENCE.txt)。libdmtx 共享库在简化版BSD许可证下分发(见 libdmtx-LICENCE.txt)。
项目详情
下载文件
下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。
源分发
构建分发
哈希值 for pylibdmtx-0.1.10-py2.py3-none-win_amd64.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e8b0140ab5f473c67fa4335333994a99ea9f683888ae892b1af2dc045560331b |
|
MD5 | aaf921de19e6568f93c2a16bfb3b2c38 |
|
BLAKE2b-256 | c14bd3fb82bd33d3b8bc0143f17c69660f99f830bd7326d0a09c3c1e1cc01771 |