python-3.x - 如何使用这种 python 格式生成条形码?
问题描述
我来问一个小问题,我安装了一些 python 模块来生成条形码(python-barcode、pybarcode、treepoem、PyUPC-EAN 等),但是生成了这个图像:
图1:
我想知道是否有某种方式或其他模块以这种方式生成代码:
图2:
这是我用于图像 1 的代码:
import barcode
from barcode.writer import ImageWriter
def genUPCA(code,coding,name):
cod = barcode.get(code, coding, writer=ImageWriter())
filename = cod.save(name)
genUPCA('upca', '123456789102', 'image')
编辑
from __future__ import absolute_import, division, print_function, unicode_literals;
import upcean
barcode = upcean.oopfuncs.barcode('upce', '01234567')
print(barcode.validate_checksum())
barcode.validate_create_barcode("./IMAGE.png", 4)
输出:假
错误:
Traceback (most recent call last):
File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\codabar.py", line 33, in create_codabar_barcode
pil_ver = Image.PILLOW_VERSION;
AttributeError: module 'PIL.Image' has no attribute 'PILLOW_VERSION'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\Tareas\T.py", line 12, in <module>
image = upcean.oopfuncs.barcode("codabar", "A1234567890B").draw_barcode(2)
File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\oopfuncs\oopfuncs.py", line 115, in draw_barcode
return upcean.barcodes.draw_barcode(self.type, self.code, size, (self.hidesn, self.hidecd, self.hidetext), self.barheight, self.textxy, (self.barcolor, self.textcolor, self.bgcolor));
File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\shortcuts.py", line 29, in draw_barcode
return create_barcode(bctype,upc,None,resize,hideinfo,barheight,textxy,barcolor);
File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\shortcuts.py", line 24, in create_barcode
return getattr(upcean.barcodes.barcode, "create_"+bctype+"_barcode")(upc,outfile,resize,hideinfo,barheight,textxy,barcolor);
File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\codabar.py", line 38, in create_codabar_barcode
pil_ver = Image.VERSION;
AttributeError: module 'PIL.Image' has no attribute 'VERSION'
解决方案
参考:
import upcean
class UPC(upcean.oopfuncs.barcode):
def __init__(self, _type, filename):
super().__init__(_type)
self.filename = f'{filename}.png'
self._digits = {'upca': 12, 'upce': 8}.get(_type, None)
def validate_create_barcode(self, upc):
self.code = upc
checksum = upc[-1]
if len(upc) < self._digits:
checksum = self.validate_checksum()
if checksum:
self.code += checksum
else:
raise ValueError(f'Invalid upc {self.code}!')
else:
valid = self.validate_checksum()
if not valid:
raise ValueError(f'Invalid upc {self.code}, validate checksum failed!')
print(f'.validate_create_barcode({self.filename}, {self.code}, checksum={checksum})')
super().validate_create_barcode(self.filename, size=2)
class UPC_A(UPC):
def __init__(self, filename=None):
if filename is None:
filename = 'upc-a'
super().__init__('upca', filename)
class UPC_E(UPC):
def __init__(self, filename=None):
if filename is None:
filename = 'upc-e'
super().__init__('upce', filename)
用法:
upca = UPC_A()
for upc in ('042100005264', '04210000526'):
upca.validate_create_barcode(upc)
upce = UPC_E()
for upc in ('06543217', '0654321'):
upca.validate_create_barcode(upc)
输出:
.validate_create_barcode(upc-a.png, 042100005264, checksum=4) .validate_create_barcode(upc-a.png, 042100005264, checksum=4).validate_create_barcode(upc-e.png, 06543217, checksum=7) .validate_create_barcode(upc-e.png, 06543217, checksum=7)
推荐阅读
- python - 如何为python文件夹中新添加的文件查找时间
- rest - Http Responding 403 Forbidden just in Post Request in GET request 没问题
- python - 将 Procfile 移动到项目根目录会使 Django 应用程序崩溃
- reactjs - Material-UI Popover 打开时无法点击背景下的元素
- spring-boot - OSGi Karaf Spring Boot 2.1.4-发布错误
- linux - 如果它以超时开始,如何退出 bash 脚本?
- javascript - 错误:无法与函数通信。请求被拒绝
- java - org.springframework.integration.dsl.filter(...) 方法发生内部异常
- amazon-web-services - 无法在 codebuild 中指定 buildsec.yml 路径
- firebase - 动态链接计数无点击