首页 > 解决方案 > 如何使用这种 python 格式生成条形码?

问题描述

我来问一个小问题,我安装了一些 python 模块来生成条形码(python-barcode、pybarcode、treepoem、PyUPC-EAN 等),但是生成了这个图像:

图1:

1.png

我想知道是否有某种方式或其他模块以这种方式生成代码:

图2:

2.jpg

这是我用于图像 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'

标签: python-3.xbarcode

解决方案


参考


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)


推荐阅读