如何使用这种 python 格式生成条形码?
Posted
技术标签:
【中文标题】如何使用这种 python 格式生成条形码?【英文标题】:how to generate barcodes with this python format? 【发布时间】:2020-09-25 20:16:18 【问题描述】:我来问一个小问题,我安装了一些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'
【问题讨论】:
您在寻找 EAN-13 格式的条码吗? 我改成 PIL.__version__; 最后打印了这个问题的版本中的图像,但是像素化了 upc-a接收12个数字,upc-e接收8个 我明白了,upc-a也是一样的 【参考方案1】:参考:
PyUPC-EAN Universal_Product_Codeimport 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,校验和=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 格式生成条形码?的主要内容,如果未能解决你的问题,请参考以下文章